Back to project page android.
The source code is released under:
Copyright (c) 2014, ganapatih All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...
If you think the Android project 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.
package helper; //w ww . j a v a 2s . co m import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; 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.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.util.Log; public abstract class RegisterAsync extends AsyncTask<String, Void, String> implements ClientInterface{ public static final int HTTP_TIMEOUT = 30 * 1000; private Context mainContext; private Activity CallerActivity; private ProgressDialog pd; private static HttpClient client; private ArrayList<NameValuePair> DataValuePair = new ArrayList<NameValuePair>(); public RegisterAsync(Context c, Activity a) { mainContext = c; CallerActivity = a; } protected void onPreExecute() { pd = new ProgressDialog(mainContext); pd.setMessage("Sending data..."); pd.show(); pd.setCancelable(false); } public abstract void onResponseReceived(String sReturn); protected String doInBackground(String... params) { TokenAsync t = new TokenAsync(mainContext, CallerActivity); String token = t.getToken(); Log.d("token", token); String url = CommonUtilities.SERVER_URL_DAFTAR; String sReturn = ""; HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("name", params[0])); pairs.add(new BasicNameValuePair("phone", params[1])); pairs.add(new BasicNameValuePair("email", params[2])); pairs.add(new BasicNameValuePair("gcmId", params[3])); pairs.add(new BasicNameValuePair("_token", token.trim())); Log.d("pairs", pairs.toString()); try { request.setEntity(new UrlEncodedFormEntity(pairs)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { str.append(line + "\n"); } in.close(); sReturn = str.toString(); } catch (Exception e) { // writing exception to log e.printStackTrace(); } Log.d("Callback server", sReturn); String callback = "false"; if(sReturn != ""){ try { JSONObject jsonObj = new JSONObject(sReturn); if(jsonObj.getString("success").equals("1")) callback = "true"; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return callback; } protected void onCancelled() { } protected void onPostExecute(String sReturn) { if (pd.isShowing()) { pd.dismiss(); } onResponseReceived(sReturn); } }