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

Java tutorial

Introduction

Here is the source code for org.kei.android.phone.mangastore.http.HttpTaskAdd.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 HttpTaskAdd.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 HttpTaskAdd<T extends FragmentActivity & HttpTaskListener> extends AsyncTask<String, Void, JSONArray> {

    public static final int CODE_EXISTS = 409;

    private final T t;

    public HttpTaskAdd(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]));
            nameValuePairs.add(new BasicNameValuePair("android_l", params[4]));
            nameValuePairs.add(new BasicNameValuePair("android_v", params[5]));
            nameValuePairs.add(new BasicNameValuePair("android_y", params[6]));
            nameValuePairs.add(new BasicNameValuePair("android_s", params[7]));
            nameValuePairs.add(new BasicNameValuePair("android_t", params[8]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            final HttpResponse response = client.execute(httpPost);
            final StatusLine statusLine = response.getStatusLine();
            final int code = statusLine.getStatusCode();
            if (code != 200) {
                if (code == CODE_EXISTS)
                    throw new IOException("" + code);
                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);
    }
}