Posted By

variable3 on 05/07/11


Tagged

json android


Versions (?)

android json application, Data retrive from a url


 / Published in: Java
 

It is application base on json data retriving from a remote URL

  1. //JsonActivity.java
  2. package com.v3;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9. import java.net.URI;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.StringTokenizer;
  13.  
  14. import org.apache.http.HttpEntity;
  15. import org.apache.http.HttpResponse;
  16. import org.apache.http.HttpStatus;
  17. import org.apache.http.client.HttpClient;
  18. import org.apache.http.client.methods.HttpGet;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.params.CoreProtocolPNames;
  21. import org.json.JSONArray;
  22. import org.json.JSONObject;
  23. import org.json.JSONStringer;
  24.  
  25. import android.app.Activity;
  26. import android.os.Bundle;
  27. import android.os.Message;
  28. import android.util.Log;
  29. import android.view.View;
  30. import android.widget.Button;
  31. import android.widget.TextView;
  32.  
  33. public class JsonActivity extends Activity {
  34. Button button,buttonClear;
  35. TextView textView;
  36. String page;
  37. String str="";
  38.  
  39. String url = "http://192.168.1.118/androidjsontest/index.php";
  40. /** Called when the activity is first created. */
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.main);
  45. button = (Button) findViewById(R.id.btnFetch);
  46. buttonClear = (Button) findViewById(R.id.btnClear);
  47. textView = (TextView) findViewById(R.id.txtView);
  48. button.setOnClickListener(new Button.OnClickListener()
  49. {
  50. public void onClick(View v)
  51. {
  52. examineJSONFile();
  53. }
  54. });
  55. buttonClear.setOnClickListener(new Button.OnClickListener()
  56. {
  57. public void onClick(View v)
  58. {
  59. textView.setText("");
  60. }
  61. });
  62.  
  63. }
  64.  
  65.  
  66. public String executeHttpGet(String URL) throws Exception
  67. {
  68. //This method for HttpConnection
  69. BufferedReader bufferedReader = null;
  70. try
  71. {
  72. HttpClient client = new DefaultHttpClient();
  73. client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
  74. HttpGet request = new HttpGet();
  75. request.setHeader("Content-Type", "text/plain; charset=utf-8");
  76. request.setURI(new URI(URL));
  77. HttpResponse response = client.execute(request);
  78. bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  79.  
  80. StringBuffer stringBuffer = new StringBuffer("");
  81. String line = "";
  82.  
  83. String NL = System.getProperty("line.separator");
  84. while ((line = bufferedReader.readLine()) != null)
  85. {
  86. stringBuffer.append(line + NL);
  87. System.out.print(stringBuffer);
  88. }
  89. bufferedReader.close();
  90. page = stringBuffer.toString();
  91. System.out.println(page+"page");
  92. return page;
  93. }
  94. finally
  95. {
  96. if (bufferedReader != null)
  97. {
  98. try
  99. {
  100. bufferedReader.close();
  101. }
  102. catch (IOException e)
  103. {
  104. Log.d("BBB", e.toString());
  105. }
  106. }
  107. }
  108. }
  109.  
  110.  
  111. void examineJSONFile()
  112. {
  113. try
  114. {
  115.  
  116. String Value="" ;
  117. String str = "";
  118. page = executeHttpGet(url);
  119. Log.i("hello", str.toString());
  120.  
  121. JSONObject object=new JSONObject(page);
  122. str=str+object.names().toString();
  123.  
  124. String sub1=str.substring(2, str.length()-2);
  125. String[] names=sub1.split("\",\"");
  126.  
  127. for (int i=0;i<names.length;i++) {
  128. Value=Value+names[i] +":"+object.get(names[i])+"\n";
  129. System.out.println(names[i]);
  130.  
  131. }
  132.  
  133. /* String sub=str.substring(1, str.length()-1);
  134.   String[] names=sub.split(",");
  135.   String[] keyValue=new String[names.length];
  136.  
  137.   for (int i=0;i<names.length;i++) {
  138.   names[i]=names[i].replace("\"","");
  139.   Value=Value+names[i] +":"+object.get(names[i])+"\n";
  140.   //keyValue[i]=names[i] +":"+object.get(names[i])+"\n";
  141. System.out.println(names[i]);
  142.  
  143. }*/
  144.  
  145. textView.setText(Value);
  146.  
  147. /*for(int i=0;i<keyValue.length;i++){
  148.   textView.setText(keyValue[i]);
  149.   }*/
  150.  
  151. Log.i("hello", str.toString());
  152.  
  153. }
  154. catch (Exception je)
  155. {
  156. textView.setText("Error w/file: " + je.getMessage());
  157. }
  158. }
  159.  
  160. }
  161. //main.xml
  162. //==================
  163. <?xml version="1.0" encoding="utf-8"?>
  164. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  165. android:orientation="vertical"
  166. android:background="#acd654"
  167. android:layout_width="fill_parent"
  168. android:layout_height="fill_parent"
  169. >
  170. <Button android:text="Fetch_Data"
  171. android:id="@+id/btnFetch"
  172. android:layout_width="match_parent"
  173. android:layout_height="wrap_content"></Button>
  174. <Button android:text="Clear_Screen"
  175. android:id="@+id/btnClear"
  176. android:layout_width="wrap_content"
  177. android:layout_height="wrap_content"></Button>
  178. <TextView android:text=""
  179. android:gravity="center"
  180. android:textColor="#000a44"
  181. android:id="@+id/txtView"
  182. android:layout_width="fill_parent"
  183. android:layout_height="fill_parent"/>
  184. </LinearLayout>
  185. //android menifest.xml
  186. //=========================
  187. ?xml version="1.0" encoding="utf-8"?>
  188. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  189. package="com.v3"
  190. android:versionCode="1"
  191. android:versionName="1.0">
  192. <uses-sdk android:minSdkVersion="8" />
  193.  
  194. <application android:icon="@drawable/icon" android:label="@string/app_name">
  195. <activity android:name=".JsonActivity"
  196. android:label="@string/app_name">
  197. <intent-filter>
  198. <action android:name="android.intent.action.MAIN" />
  199. <category android:name="android.intent.category.LAUNCHER" />
  200. </intent-filter>
  201. </activity>
  202.  
  203. </application>
  204. <uses-permission android:name="android.permission.INTERNET"/>
  205. </manifest>

Report this snippet  

You need to login to post a comment.