Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. package com.gikdew.emaildemo;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  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 android.app.ProgressDialog;
  13. import android.content.Intent;
  14. import android.content.SharedPreferences;
  15. import android.content.SharedPreferences.Editor;
  16. import android.os.AsyncTask;
  17. import android.os.Bundle;
  18. import android.support.v7.app.ActionBarActivity;
  19. import android.util.Log;
  20. import android.view.Menu;
  21. import android.view.MenuInflater;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.widget.ListView;
  25.  
  26. public class App extends ActionBarActivity {
  27.  
  28. private ProgressDialog pDialog;
  29.  
  30. // Creating JSON Parser object
  31. JSONParser jParser = new JSONParser();
  32.  
  33. ArrayList<HashMap<String, String>> productsList;
  34.  
  35. // url to get all products list
  36. private static String url_all_products = "http://gikdew.com/android/emails/get_all_signals.php";
  37.  
  38. // JSON Node names
  39. private static final String TAG_SUCCESS = "success";
  40. private static final String TAG_PRODUCTS = "signals";
  41. private static final String TAG_ID = "id";
  42. private static final String TAG_pair = "pair";
  43. private static final String TAG_expiry = "expiry";
  44. private static final String TAG_result = "result";
  45. private static final String TAG_created_at = "created_at";
  46.  
  47. ListView list;
  48.  
  49. // products JSONArray
  50. JSONArray products = null;
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. // TODO Auto-generated method stub
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.app);
  57.  
  58. list = (ListView) findViewById(R.id.listview1);
  59. list.addHeaderView(new View(this));
  60. list.addFooterView(new View(this));
  61.  
  62. productsList = new ArrayList<HashMap<String, String>>();
  63.  
  64. // Loading products in Background Thread
  65. new LoadAllProducts().execute();
  66.  
  67. }
  68.  
  69. @Override
  70. public boolean onCreateOptionsMenu(Menu menu) {
  71. MenuInflater inflater = getMenuInflater();
  72. inflater.inflate(R.menu.main, menu);
  73. return true;
  74. }
  75.  
  76. @Override
  77. public boolean onOptionsItemSelected(MenuItem item) {
  78. // Handle item selection
  79. switch (item.getItemId()) {
  80. case R.id.action_refresh:
  81. new LoadAllProducts().execute();
  82. return true;
  83. default:
  84. return super.onOptionsItemSelected(item);
  85. }
  86. }
  87.  
  88. class LoadAllProducts extends AsyncTask<String, String, String> {
  89.  
  90. /**
  91. * Before starting background thread Show Progress Dialog
  92. * */
  93.  
  94. BaseInflaterAdapter<ItemData> adapter;
  95.  
  96. @Override
  97. protected void onPreExecute() {
  98. super.onPreExecute();
  99. pDialog = new ProgressDialog(App.this);
  100. pDialog.setMessage("Loading signals...");
  101. pDialog.setIndeterminate(false);
  102. pDialog.setCancelable(false);
  103. pDialog.show();
  104. adapter = new BaseInflaterAdapter<ItemData>(new CardInflater());
  105. }
  106.  
  107. /**
  108. * getting All products from url
  109. * */
  110. protected String doInBackground(String... args) {
  111. // Building Parameters
  112. List<NameValuePair> params = new ArrayList<NameValuePair>();
  113. // getting JSON string from URL
  114. JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
  115. params);
  116.  
  117. // Check your log cat for JSON reponse
  118. Log.d("All Products: ", json.toString());
  119.  
  120. try {
  121. // Checking for SUCCESS TAG
  122. int success = json.getInt(TAG_SUCCESS);
  123.  
  124. if (success == 1) {
  125. // products found
  126. // Getting Array of Products
  127. products = json.getJSONArray(TAG_PRODUCTS);
  128.  
  129. // looping through All Products
  130. for (int i = 0; i < products.length(); i++) {
  131. JSONObject c = products.getJSONObject(i);
  132.  
  133. // Storing each json item in variable
  134. String id = c.getString(TAG_ID);
  135. String pair = c.getString(TAG_pair);
  136. String expiry = c.getString(TAG_expiry);
  137. String result = c.getString(TAG_result);
  138. String createat = c.getString(TAG_created_at);
  139.  
  140. ItemData data = new ItemData(id, pair, expiry, result,
  141. createat);
  142. adapter.addItem(data, false);
  143. adapter.notifyDataSetChanged();
  144.  
  145. if (i == 0) {
  146. SharedPreferences signalsPref = getSharedPreferences(
  147. "signals", 0);
  148. Editor editor = signalsPref.edit();
  149.  
  150. editor.putString("lastSignal", id);
  151. editor.commit();
  152.  
  153. Log.d("lastSignal",
  154. signalsPref.getString("lastSignal", "0"));
  155. }
  156. }
  157. } else {
  158. // no products found
  159. // Launch Add New product Activity
  160. Intent i = new Intent(getApplicationContext(), App.class);
  161. // Closing all previous activities
  162. i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  163. startActivity(i);
  164. }
  165. } catch (JSONException e) {
  166. e.printStackTrace();
  167. }
  168.  
  169. return null;
  170. }
  171.  
  172. /**
  173. * After completing background task Dismiss the progress dialog
  174. * **/
  175. protected void onPostExecute(String file_url) {
  176. // dismiss the dialog after getting all products
  177. pDialog.dismiss();
  178. // updating UI from Background Thread
  179. runOnUiThread(new Runnable() {
  180. public void run() {
  181.  
  182. list.setAdapter(adapter);
  183. }
  184. });
  185.  
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement