Android Open Source - ddgatve-android Post Async Task






From Project

Back to project page ddgatve-android.

License

The source code is released under:

Apache License

If you think the Android project ddgatve-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package lv.ddgatve.math.main;
/*from   ww  w . j ava 2 s .co m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;

public class PostAsyncTask extends AsyncTask<String, String, Void> {

  private RegisterActivity activity;
  private ProgressDialog progressDialog;
  InputStream inputStream = null;
  String result = "";

  public PostAsyncTask(RegisterActivity activity) {
    this.activity = activity;
    progressDialog = new ProgressDialog(activity);
  }

  protected void onPreExecute() {
    progressDialog.setMessage("Downloading your data...");
    progressDialog.show();
    progressDialog.setOnCancelListener(new OnCancelListener() {
      public void onCancel(DialogInterface arg0) {
        PostAsyncTask.this.cancel(true);
      }
    });
  }

  @Override
  protected Void doInBackground(String... params) {

    String url_select = "http://echo.jsontest.com/firstname/" + params[0]
        + "/lastname/" + params[1] + "/email/" + params[2]
        + "/password/" + params[3];

    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

    try {
      // Set up HTTP post

      // HttpClient is more then less deprecated. Need to change to
      // URLConnection
      HttpClient httpClient = new DefaultHttpClient();

      HttpPost httpPost = new HttpPost(url_select);
      httpPost.setEntity(new UrlEncodedFormEntity(param));
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();

      // Read content & Log
      inputStream = httpEntity.getContent();
    } catch (UnsupportedEncodingException e1) {
      Log.e("UnsupportedEncodingException", e1.toString());
      e1.printStackTrace();
    } catch (ClientProtocolException e2) {
      Log.e("ClientProtocolException", e2.toString());
      e2.printStackTrace();
    } catch (IllegalStateException e3) {
      Log.e("IllegalStateException", e3.toString());
      e3.printStackTrace();
    } catch (IOException e4) {
      Log.e("IOException", e4.toString());
      e4.printStackTrace();
    }
    // Convert response to string using String Builder
    try {
      BufferedReader bReader = new BufferedReader(new InputStreamReader(
          inputStream, "iso-8859-1"), 8);
      StringBuilder sBuilder = new StringBuilder();

      String line = null;
      while ((line = bReader.readLine()) != null) {
        sBuilder.append(line + "\n");
      }
      inputStream.close();
      result = sBuilder.toString();

    } catch (Exception e) {
      Log.e("StringBuilding & BufferedReader", "Error converting result "
          + e.toString());
    }
    return null;
  }

  protected void onPostExecute(Void v) {
    try {
      JSONObject reader = new JSONObject(result);
      String key = reader.getString("firstname");
      this.progressDialog.dismiss();
      result = key;

    } catch (JSONException e) {
      Log.e("JSONException", "Error: " + e.toString());
    }
    activity.displayResult(result);
  }
}




Java Source Code List

lv.ddgatve.applets.ColoredCircleApplet.java
lv.ddgatve.applets.MovingCircleApplet.java
lv.ddgatve.applets.PlainCircleApplet.java
lv.ddgatve.applets.SmoothCircleApplet.java
lv.ddgatve.games.game15.Game15Activity.java
lv.ddgatve.games.game15.Game15Frame.java
lv.ddgatve.games.game15.ImageAdapter.java
lv.ddgatve.games.game15.PickFrameDialogFragment.java
lv.ddgatve.games.game15.SummaryActivity.java
lv.ddgatve.games.main.MainActivity.java
lv.ddgatve.games.mtable.DataHolder.java
lv.ddgatve.games.mtable.DisplayMessageActivity.java
lv.ddgatve.games.mtable.MainActivity.java
lv.ddgatve.games.mtable.Questions.java
lv.ddgatve.games.mtable.SummaryActivity.java
lv.ddgatve.math.main.ExampleActivity.java
lv.ddgatve.math.main.MainActivity.java
lv.ddgatve.math.main.PostAsyncTask.java
lv.ddgatve.math.main.RegisterActivity.java
lv.hello.HelloWorld.java
lv.hello.MultiplicationTable.java
lv.kapsitis.myfirstapp.MainActivity.java