Advertisement
euis_kusesa

ServiceAll

Dec 24th, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. package com.example.asusx441n.examplelistviewdinamis;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.os.AsyncTask;
  6.  
  7. import org.apache.http.NameValuePair;
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15.  
  16. public class ServiceAll extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
  17.     Global global = new Global();
  18.     private ProgressDialog pDialog;
  19.     Context context;
  20.     int success;
  21.     String message;
  22.     private JSONParser jsonParser = new JSONParser();
  23.     JSONArray orders = null;
  24.     ArrayList<HashMap<String, String>> isExist;
  25.  
  26.  
  27.     public ServiceAll(Context context) {
  28.         this.context = context;
  29.     }
  30.     @Override
  31.     protected void onPreExecute() {
  32.         super.onPreExecute();
  33.         pDialog = new ProgressDialog(context);
  34.         pDialog.setMessage("Add to database");
  35.         pDialog.setIndeterminate(false);
  36.         pDialog.setCancelable(true);
  37.         //pDialog.show();
  38.     }
  39.  
  40.  
  41.     @Override
  42.     protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
  43.  
  44.         // Building Parameters
  45.         List<NameValuePair> param = new ArrayList<NameValuePair>();
  46.  
  47.         JSONObject json = null;
  48.  
  49.         //param.add(new BasicNameValuePair("whattodo", params[0]));
  50.  
  51.         json = jsonParser.makeHttpRequest(global.URL_GET_CRUD, "POST", param);
  52.  
  53.         isExist = new ArrayList<HashMap<String, String>>();
  54.  
  55.  
  56.         try {
  57.  
  58.             success = json.getInt(global.TAG_SUCCESS);
  59.             message = json.getString(global.TAG_MESSAGE);
  60.  
  61.             if (success == 1) {
  62.  
  63.                 // Getting Array of Return Value Login
  64.                 orders = json.getJSONArray("ceritarakyat");
  65.  
  66.                 // looping through All Return Value
  67.                 for (int i = 0; i < orders.length(); i++) {
  68.                     JSONObject c = orders.getJSONObject(i);
  69.  
  70.                     // Storing each json item in variable
  71.                     String id = c.getString(global.DATA_ID_STORY);
  72.                     String nik = c.getString(global.DATA_TITLE);
  73.                     String name = c.getString(global.DATA_PROVINSI);
  74.                     String note = c.getString(global.DATA_IMAGE1);
  75.                     String note2 = c.getString(global.DATA_IMAGE2);
  76.                     String content = c.getString(global.DATA_CONTENT);
  77.                     String autor = c.getString(global.DATA_AUTHOR);
  78.                     String outcame = c.getString(global.DATA_OUTCOME);
  79.  
  80.  
  81.                     // creating new HashMap
  82.                     HashMap<String, String> map = new HashMap<String, String>();
  83.  
  84.                     // adding each child node to HashMap key => value
  85.  
  86.                     map.put(global.DATA_ID_STORY, id);
  87.                     map.put(global.DATA_TITLE, nik);
  88.                     map.put(global.DATA_PROVINSI, name);
  89.                     map.put(global.DATA_IMAGE1, note);
  90.                     map.put(global.DATA_IMAGE2, note2);
  91.                     map.put(global.DATA_CONTENT, content);
  92.                     map.put(global.DATA_AUTHOR, autor);
  93.                     map.put(global.DATA_OUTCOME, outcame);
  94.  
  95.  
  96.                     // adding HashList to ArrayList
  97.                     isExist.add(map);
  98.                 }
  99.             } else {
  100.                 return null;
  101.             }
  102.  
  103.         } catch (JSONException e) {
  104.             e.printStackTrace();
  105.         }
  106.  
  107.         return isExist;
  108.  
  109.     }
  110.  
  111.  
  112.     /**
  113.      * After completing background task Dismiss the progress dialog
  114.      **/
  115.     protected void onPostExecute(ArrayList<HashMap<String, String>> file_url) {
  116.  
  117.         pDialog.dismiss();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement