1. php中

echo json_encode($data);

2. Android中

package android.test; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class Main extends Activity { private TextView m_textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); JSONObject obj = GetJsonObject(); StringBuilder sb = new StringBuilder(); try { sb.append("product_id: " + obj.getInt("id") + "/n"); sb.append("website_name: " + obj.getString("site_name") + "/n"); sb.append("title: " + obj.getString("title") + "/n"); } catch (JSONException e) { e.printStackTrace(); } m_textView = (TextView) findViewById(R.id.myTextView); m_textView.setText(sb.toString()); } private JSONObject GetJsonObject() { HttpClient client = new DefaultHttpClient(); StringBuilder builder = new StringBuilder(); JSONArray jsonArray = null; HttpGet get = new HttpGet("http://www.test.com/aaa"); try { HttpResponse response = client.execute(get); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); for (String s = reader.readLine(); s != null; s = reader.readLine()) { builder.append(s); } Log.i("json_str", builder.toString()); jsonArray = new JSONArray(builder.toString()); for (int i = 0; i < 2; ++i) { JSONObject jsonObject = jsonArray.getJSONObject(i); Log.i("id", jsonObject.getInt("id") + ""); Log.i("website_name", jsonObject.getString("site_name")); Log.i("website_url", jsonObject.getString("site_url")); Log.i("category", jsonObject.getInt("category") + ""); Log.i("title", jsonObject.getString("title")); } } catch (Exception e) { e.printStackTrace(); } try { return jsonArray.getJSONObject(5); } catch (JSONException e) { e.printStackTrace(); return null; } } }

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 数据结构基础知识核心归纳(三)
  2. Android(安卓)UI—仿微信底部导航栏布局
  3. 67-Flutter中高德地图插件的使用
  4. Macaca安装配置流程
  5. [Android学UI之三]实现新浪微博消息页面
  6. Android开发之如何在App间安全地共享文件
  7. Android(安卓)ExpandableListView简单使
  8. android 自定义View绘制电池电量(电池内带
  9. android代码中打开系统设置界面 .
  10. Android(安卓)学习笔记之界面布局