Posted By

VijayDePrabu on 03/02/13


Tagged

jason json how android


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

VijayDePrabu


JSON How


 / Published in: Java
 

URL: http://vijaydeprabu.blogspot.in/2013/03/vijaya-prabu-json-how.html

this code is for json parsing

  1. package com.vj.activitycallbacks;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.ClientProtocolException;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpGet;
  10. import org.apache.http.impl.client.DefaultHttpClient;
  11. import org.apache.http.util.EntityUtils;
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import android.app.Activity;
  17. import android.content.Intent;
  18. import android.os.AsyncTask;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.Menu;
  22. import android.view.View;
  23. import android.widget.TextView;
  24.  
  25.  
  26. public class MainActivity extends Activity {
  27.  
  28. JSONObject ja;
  29. HttpClient client;
  30. TextView tv1;
  31. private String result;
  32. final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
  33. public static final String TAG = "com.vj.activitycallbacks.MainActivity";
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. tv1 = (TextView) findViewById(R.id.textView1);
  40. Log.i(TAG, "onCreate");
  41.  
  42. new MyAsyncTask().execute("text");
  43.  
  44. }
  45. public JSONObject lastTweet(String name) throws ClientProtocolException,
  46. IOException, JSONException {
  47. client=new DefaultHttpClient();
  48. StringBuilder url = new StringBuilder(URL);
  49. Log.e(TAG, "status"+1);
  50. url.append(name);
  51. Log.e(TAG, "status"+2);
  52. HttpGet hg = new HttpGet(url.toString());
  53. Log.e(TAG, "status"+3);
  54. HttpResponse r = client.execute(hg);
  55. Log.e(TAG, "status"+4);
  56. int status = r.getStatusLine().getStatusCode();
  57. Log.e(TAG, "status"+status);
  58. if (status == 200) {
  59. HttpEntity ent = r.getEntity();
  60. String data = EntityUtils.toString(ent);
  61. JSONArray jarray = new JSONArray(data);
  62. ja = jarray.getJSONObject(0);
  63. Log.e(TAG, "success fetching data");
  64. return ja;
  65.  
  66. } else {
  67. Log.e(TAG, "some error");
  68. return null;
  69. }
  70. }
  71.  
  72.  
  73. @Override
  74. protected void onRestart() {
  75. // TODO Auto-generated method stub
  76. super.onRestart();
  77. Log.i(TAG, "onRestart");
  78. }
  79.  
  80. @Override
  81. protected void onStart() {
  82. // TODO Auto-generated method stub
  83. super.onStart();
  84. Log.i(TAG, "onStart");
  85. }
  86.  
  87. @Override
  88. protected void onRestoreInstanceState(Bundle savedInstanceState) {
  89. // TODO Auto-generated method stub
  90. super.onRestoreInstanceState(savedInstanceState);
  91. Log.i(TAG, "onRestoreInstanceState");
  92. }
  93.  
  94. @Override
  95. protected void onResume() {
  96. // TODO Auto-generated method stub
  97. super.onResume();
  98. Log.i(TAG, "onResume");
  99. }
  100.  
  101. @Override
  102. protected void onPause() {
  103. // TODO Auto-generated method stub
  104. super.onPause();
  105. Log.i(TAG, "onPause");
  106. }
  107.  
  108. @Override
  109. protected void onSaveInstanceState(Bundle outState) {
  110. // TODO Auto-generated method stub
  111. super.onSaveInstanceState(outState);
  112. Log.i(TAG, "onSaveInstanceState");
  113. }
  114.  
  115. @Override
  116. protected void onStop() {
  117. // TODO Auto-generated method stub
  118. super.onStop();
  119. Log.i(TAG, "onStop");
  120. }
  121.  
  122. @Override
  123. protected void onDestroy() {
  124. // TODO Auto-generated method stub
  125. super.onDestroy();
  126. Log.i(TAG, "onDestroy");
  127. }
  128.  
  129. @Override
  130. public boolean onCreateOptionsMenu(Menu menu) {
  131. Log.i(TAG, "");
  132. getMenuInflater().inflate(R.menu.activity_main, menu);
  133. return true;
  134. }
  135.  
  136. public void onListActivity(View v) {
  137. Intent in = new Intent(MainActivity.this, ListActivity.class);
  138. }
  139.  
  140.  
  141. public class MyAsyncTask extends AsyncTask<String, Integer, String> {
  142.  
  143. @Override
  144. protected String doInBackground(String... params) {
  145. String result = null;
  146. try {
  147. ja = lastTweet("vijaydeprabu1");
  148.  
  149. result = ja.getString("text");
  150. return result;
  151.  
  152.  
  153. } catch (ClientProtocolException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. Log.e(TAG, e.toString());
  157. } catch (IOException e) {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. Log.e(TAG, e.toString());
  161. } catch (JSONException e) {
  162.  
  163. e.printStackTrace();
  164. Log.e(TAG, e.toString());
  165. }
  166. return null;
  167.  
  168.  
  169. }
  170.  
  171. @Override
  172. protected void onPostExecute(String result1) {
  173. tv1.setText(result1);
  174.  
  175. }
  176.  
  177. }
  178.  
  179. }

Report this snippet  

You need to login to post a comment.