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

Java tutorial

Introduction

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

Source

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
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.Fragment;

/**
 *******************************************************************************
 * @file HttpTaskFetch.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 HttpTaskFetch<T extends Fragment & HttpTaskListener> extends AsyncTask<String, Void, JSONArray> {

    private final T t;

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

    @Override
    protected JSONArray doInBackground(final String... params) {
        JSONArray result = null;
        BufferedReader reader = null;
        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]));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            final HttpResponse response = client.execute(httpPost);
            final StatusLine statusLine = response.getStatusLine();
            final int code = statusLine.getStatusCode();
            if (code == 200) {
                final StringBuilder sb = new StringBuilder();
                final HttpEntity entity = response.getEntity();
                final InputStream content = entity.getContent();
                reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null)
                    sb.append(line);
                result = new JSONArray(sb.toString());
            } else {
                throw new IOException(
                        "The server has responded an error " + code + " (" + statusLine.getReasonPhrase() + ")");
            }
        } catch (final Exception e) {
            t.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    t.onTaskError(e);
                }
            });
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                }
            }
        }
        return result;
    }

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