org.kei.android.phone.mangastore.http.HttpTaskRemove.java Source code

Java tutorial

Introduction

Here is the source code for org.kei.android.phone.mangastore.http.HttpTaskRemove.java

Source

package org.kei.android.phone.mangastore.http;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;

/**
 *******************************************************************************
 * @file HttpTaskRemove.java
 * @author Keidan
 * @date 15/04/2016
 * @par Project MangaStore
 *
 * @par Copyright 2016 Keidan, all right reserved
 *
 *      This software is distributed in the hope that it will be useful, but
 *      WITHOUT ANY WARRANTY.
 *
 *      License summary : You can modify and redistribute the sources code and
 *      binaries. You can send me the bug-fix
 *
 *      Term of the license in in the file license.txt.
 *
 *******************************************************************************
 */
@SuppressWarnings("deprecation")
public class HttpTaskRemove<T extends FragmentActivity & HttpTaskListener>
        extends AsyncTask<String, Void, JSONArray> {

    private final T t;

    public HttpTaskRemove(final T t) {
        this.t = t;
    }

    @Override
    protected JSONArray doInBackground(final String... params) {
        try {
            final HttpClient client = new DefaultHttpClient();
            final HttpPost httpPost = new HttpPost(params[0]);
            final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("android_name", params[1]));
            nameValuePairs.add(new BasicNameValuePair("android_password", params[2]));
            nameValuePairs.add(new BasicNameValuePair("android_i", params[3]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            final HttpResponse response = client.execute(httpPost);
            final StatusLine statusLine = response.getStatusLine();
            final int code = statusLine.getStatusCode();
            if (code != 200) {
                throw new IOException("The server has responded an error " + code);
            }
        } catch (final Exception e) {
            t.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    t.onTaskError(e);
                }
            });
        }
        return null;
    }

    @Override
    protected void onPostExecute(final JSONArray response) {
        t.onTaskResponse(response);
    }
}