Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import android.app.Activity;
  7. import android.app.ProgressDialog;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.Button;
  13. import android.widget.ListAdapter;
  14. import android.widget.ListView;
  15. import android.widget.SimpleAdapter;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. public class MainActivity extends Activity {
  20. ListView list;
  21. TextView ver;
  22. TextView name;
  23. TextView api;
  24. Button Btngetdata;
  25. ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
  26. //URL to get JSON Array
  27. private static String url = "*****";
  28. //JSON Node Names
  29. private static final String TAG_OS = "android";
  30. private static final String TAG_VER = "ver";
  31. private static final String TAG_NAME = "name";
  32. private static final String TAG_API = "api";
  33. JSONArray android = null;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. oslist = new ArrayList<HashMap<String, String>>();
  39. Btngetdata = (Button)findViewById(R.id.getdata);
  40. Btngetdata.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View view) {
  43. new JSONParse().execute();
  44. }
  45. });
  46. }
  47. private class JSONParse extends AsyncTask<String, String, JSONObject> {
  48. private ProgressDialog pDialog;
  49. @Override
  50. protected void onPreExecute() {
  51. super.onPreExecute();
  52. ver = (TextView)findViewById(R.id.vers);
  53. name = (TextView)findViewById(R.id.name);
  54. api = (TextView)findViewById(R.id.api);
  55. pDialog = new ProgressDialog(MainActivity.this);
  56. pDialog.setMessage("Getting Data ...");
  57. pDialog.setIndeterminate(false);
  58. pDialog.setCancelable(true);
  59. pDialog.show();
  60. }
  61. @Override
  62. protected JSONObject doInBackground(String... args) {
  63. JSONParser jParser = new JSONParser();
  64. // Getting JSON from URL
  65. JSONObject json = jParser.getJSONFromUrl(url);
  66. return json;
  67. }
  68. @Override
  69. protected void onPostExecute(JSONObject json) {
  70. pDialog.dismiss();
  71. try {
  72. // Getting JSON Array from URL
  73. android = json.getJSONArray(TAG_OS);
  74. for(int i = 0; i < android.length(); i++){
  75. JSONObject c = android.getJSONObject(i);
  76. // Storing JSON item in a Variable
  77. String ver = c.getString(TAG_VER);
  78. String name = c.getString(TAG_NAME);
  79. String api = c.getString(TAG_API);
  80. // Adding value HashMap key => value
  81. HashMap<String, String> map = new HashMap<String, String>();
  82. map.put(TAG_VER, ver);
  83. map.put(TAG_NAME, name);
  84. map.put(TAG_API, api);
  85. oslist.add(map);
  86. list=(ListView)findViewById(R.id.list);
  87. ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
  88. R.layout.list_v,
  89. new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
  90. R.id.vers,R.id.name, R.id.api});
  91. list.setAdapter(adapter);
  92. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  93. @Override
  94. public void onItemClick(AdapterView<?> parent, View view,
  95. int position, long id) {
  96. Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
  97. }
  98. });
  99. }
  100. } catch (JSONException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. }
  106.  
  107. import java.io.BufferedReader;
  108. import java.io.IOException;
  109. import java.io.InputStream;
  110. import java.io.InputStreamReader;
  111. import java.io.UnsupportedEncodingException;
  112. import org.apache.http.HttpEntity;
  113. import org.apache.http.HttpResponse;
  114. import org.apache.http.client.ClientProtocolException;
  115. import org.apache.http.client.methods.HttpPost;
  116. import org.apache.http.impl.client.DefaultHttpClient;
  117. import org.json.JSONException;
  118. import org.json.JSONObject;
  119. import android.util.Log;
  120. public class JSONParser {
  121. static InputStream is = null;
  122. static JSONObject jObj = null;
  123. static String json = "";
  124. // constructor
  125. public JSONParser() {
  126. }
  127. public JSONObject getJSONFromUrl(String url) {
  128. // Making HTTP request
  129. try {
  130. // defaultHttpClient
  131. DefaultHttpClient httpClient = new DefaultHttpClient();
  132. HttpPost httpPost = new HttpPost(url);
  133. HttpResponse httpResponse = httpClient.execute(httpPost);
  134. HttpEntity httpEntity = httpResponse.getEntity();
  135. is = httpEntity.getContent();
  136. } catch (UnsupportedEncodingException e) {
  137. e.printStackTrace();
  138. } catch (ClientProtocolException e) {
  139. e.printStackTrace();
  140. } catch (IOException e) {
  141. e.printStackTrace();
  142. }
  143. try {
  144. BufferedReader reader = new BufferedReader(new InputStreamReader(
  145. is, "iso-8859-1"), 8);
  146. StringBuilder sb = new StringBuilder();
  147. String line = null;
  148. while ((line = reader.readLine()) != null) {
  149. sb.append(line + "n");
  150. }
  151. is.close();
  152. json = sb.toString();
  153. } catch (Exception e) {
  154. Log.e("Buffer Error", "Error converting result " + e.toString());
  155. }
  156. // try parse the string to a JSON object
  157. try {
  158. jObj = new JSONObject(json);
  159. } catch (JSONException e) {
  160. Log.e("JSON Parser", "Error parsing data " + e.toString());
  161. }
  162. // return JSON String
  163. return jObj;
  164. }
  165. }
  166.  
  167. List<list> varr = yourlist;
  168. for (list result : varr) {
  169. // action such as.. if result
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement