Advertisement
Guest User

Example

a guest
Jun 4th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package com.gabilheri.formulacalculator.main;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. /**
  16.  * @author Marcus Gabilheri
  17.  * @version 1.0
  18.  * @since 6/4/14
  19.  */
  20. public class Example extends Activity {
  21.  
  22.     private String URL = "http://myUrl/myFile";
  23.     private JSONParser jsonParser;
  24.     private ProgressDialog pDialog;
  25.  
  26.     private static final String TAG_SUCCESS = "success";
  27.     private static final String TAG_MESSAGE = "message";
  28.  
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.  
  34.         new MyTask().execute();
  35.     }
  36.  
  37.     @Override
  38.     protected void onResume() {
  39.         super.onResume();
  40.     }
  41.  
  42.     class MyTask extends AsyncTask<Object, Object, Object> {
  43.  
  44.         @Override
  45.         protected void onPreExecute() {
  46.             super.onPreExecute();
  47.             // Do stuff here before the request such as showing progress bar, dialogs, etc.
  48.             pDialog = new ProgressDialog(Example.this);
  49.             pDialog.setMessage("Loading Questions...");
  50.             pDialog.setIndeterminate(false);
  51.             pDialog.setCancelable(true);
  52.             pDialog.show();
  53.  
  54.         }
  55.  
  56.         @Override
  57.         protected Object doInBackground(Object... params) {
  58.  
  59.             // Do the network request here
  60.             int success = 0;
  61.             try {
  62.  
  63.                 List<NameValuePair> stuff = new ArrayList<NameValuePair>();
  64.                 stuff.add(new BasicNameValuePair("label",  "data"));
  65.  
  66.                 JSONObject jObject = jsonParser.makeHttpRequest(URL, "POST", stuff);
  67.  
  68.                 success = jObject.getInt(TAG_SUCCESS);
  69.  
  70.                 if(success == 1) {
  71.  
  72.                     // You got your stuff!
  73.                     // Note that I sent an success field with an integer from my database
  74.                     // you can handle it anyway you want
  75.                     // But is nice to have a flag that tells you if it was successfull or not.
  76.  
  77.                 }
  78.  
  79.  
  80.             } catch (JSONException e) {
  81.                 e.printStackTrace();
  82.             }
  83.  
  84.             return null;
  85.  
  86.         }
  87.  
  88.         @Override
  89.         protected void onPostExecute(Object o) {
  90.             super.onPostExecute(o);
  91.             // Handle your UI updates with the data from the request here
  92.             pDialog.dismiss();
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement