[Android教程] android获得json数据并处理

来源:互联网 发布:手机西西软件盒安卓 编辑:程序博客网 时间:2024/05/21 09:18

android获得json数据并处理:

  1. //JsonActivity.java
  2. package cn.outofmemory.android;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.Reader;
  8. import java.net.URI;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.StringTokenizer;
  12. http://www.nvzi91.cn/fujianyan/30059.html
  13. import org.apache.http.HttpEntity;
  14. import org.apache.http.HttpResponse;
  15. import org.apache.http.HttpStatus;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.params.CoreProtocolPNames;
  20. import org.json.JSONArray;
  21. import org.json.JSONObject;
  22. import org.json.JSONStringer;
  23. http://www.nvzi91.cn/chunvmoxiufu/30060.html
  24. import android.app.Activity;
  25. import android.os.Bundle;
  26. import android.os.Message;
  27. import android.util.Log;
  28. import android.view.View;
  29. import android.widget.Button;
  30. import android.widget.TextView;
  31. public class JsonActivity extends Activity {
  32. Button button,buttonClear;
  33. TextView textView;
  34. String page;
  35. String str="";
  36. http://www.nvzi91.cn/gongjingjibing/30061.html
  37. String url = "http://192.168.1.118/androidjsontest/index.php";
  38. /** Called when the activity is first created. */
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.main);
  43. button = (Button) findViewById(R.id.btnFetch);
  44. buttonClear = (Button) findViewById(R.id.btnClear);
  45. textView = (TextView) findViewById(R.id.txtView);
  46. button.setOnClickListener(new Button.OnClickListener()
  47. {
  48. public void onClick(View v)
  49. {
  50. examineJSONFile();
  51. }
  52. });
  53. buttonClear.setOnClickListener(new Button.OnClickListener()
  54. {
  55. public void onClick(View v)
  56. {
  57. textView.setText("");
  58. }
  59. });
  60. }
  61. http://www.nvzi91.cn/fujianyan/30062.html
  62. public String executeHttpGet(String URL) throws Exception
  63. {
  64. //This method for HttpConnection
  65. BufferedReader bufferedReader = null;
  66. try
  67. {
  68. HttpClient client = new DefaultHttpClient();
  69. client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
  70. HttpGet request = new HttpGet();
  71. request.setHeader("Content-Type", "text/plain; charset=utf-8");
  72. request.setURI(new URI(URL));
  73. HttpResponse response = client.execute(request);
  74. bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  75. StringBuffer stringBuffer = new StringBuffer("");
  76. String line = "";
  77. http://www.nvzi91.cn/fujianyan/30063.html
  78. String NL = System.getProperty("line.separator");
  79. while ((line = bufferedReader.readLine()) != null)
  80. {
  81. stringBuffer.append(line + NL);
  82. System.out.print(stringBuffer);
  83. }
  84. bufferedReader.close();
  85. page = stringBuffer.toString();
  86. System.out.println(page+"page");
  87. return page;
  88. }
  89. finally
  90. {
  91. if (bufferedReader != null)
  92. {
  93. try
  94. {
  95. bufferedReader.close();
  96. }
  97. catch (IOException e)
  98. {
  99. Log.d("BBB", e.toString());
  100. }
  101. }
  102. }
  103. }
  104. www.nvzi91.cn
  105. void examineJSONFile()
  106. {
  107. try
  108. {
  109. String Value="" ;
  110. String str = "";
  111. page = executeHttpGet(url);
  112. Log.i("hello", str.toString());
  113. JSONObject object=new JSONObject(page);
  114. str=str+object.names().toString();
  115. String sub1=str.substring(2, str.length()-2);
  116. String[] names=sub1.split("\",\"");
  117. for (int i=0;i<names.length;i++) {
  118. Value=Value+names[i] +":"+object.get(names[i])+"\n";
  119. System.out.println(names[i]);
  120. }
  121. /* String sub=str.substring(1, str.length()-1);
  122. String[] names=sub.split(",");
  123. String[] keyValue=new String[names.length];
  124. for (int i=0;i<names.length;i++) {
  125. names[i]=names[i].replace("\"","");
  126. Value=Value+names[i] +":"+object.get(names[i])+"\n";
  127. //keyValue[i]=names[i] +":"+object.get(names[i])+"\n";
  128. System.out.println(names[i]);
  129. }*/
  130. textView.setText(Value);
  131. /*for(int i=0;i<keyValue.length;i++){
  132. textView.setText(keyValue[i]);
  133. }*/
  134. Log.i("hello", str.toString());
  135. }
  136. catch (Exception je)
  137. {
  138. textView.setText("Error w/file: " + je.getMessage());
  139. }
  140. }
  141. }
复制代码

main.xml代码:

  1. //==================
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:background="#acd654"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. >
  9. <Button android:text="Fetch_Data"
  10. android:id="@+id/btnFetch"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"></Button>
  13. <Button android:text="Clear_Screen"
  14. android:id="@+id/btnClear"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"></Button>
  17. <TextView android:text=""
  18. android:gravity="center"
  19. android:textColor="#000a44"
  20. android:id="@+id/txtView"
  21. android:layout_width="fill_parent"
  22. android:layout_height="fill_parent"/>
  23. </LinearLayout>
复制代码

android menifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.outofmemory.android"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="8" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".JsonActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. <uses-permission android:name="android.permission.INTERNET"/>
  17. </manifest>
0 0
原创粉丝点击