发布于 2016-03-11 06:54:11 | 146 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现读写JSON数据的方法,以完整实例形式分析了Android解析及生成json数据的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android实现读写JSON数据的方法。分享给大家供大家参考。具体如下:

1. 解析JSON:


package de.vogella.android.twitter.json;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
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.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class ParseJSON extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  String readTwitterFeed = readTwitterFeed();
  try {
   JSONArray jsonArray = new JSONArray(readTwitterFeed);
   Log.i(ParseJSON.class.getName(),
     "Number of entries " + jsonArray.length());
   for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 public String readTwitterFeed() {
  StringBuilder builder = new StringBuilder();
  HttpClient client = new DefaultHttpClient();
  HttpGet httpGet = new HttpGet(
    "http://twitter.com/statuses/user_timeline/vogella.json");
  try {
   HttpResponse response = client.execute(httpGet);
   StatusLine statusLine = response.getStatusLine();
   int statusCode = statusLine.getStatusCode();
   if (statusCode == 200) {
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(
      new InputStreamReader(content));
    String line;
    while ((line = reader.readLine()) != null) {
     builder.append(line);
    }
   } else {
    Log.e(ParseJSON.class.toString(), "Failed to download file");
   }
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return builder.toString();
 }
}

2. 生成JSON:


public void writeJSON() {
 JSONObject object = new JSONObject();
 try {
  object.put("name", "Jack Hack");
  object.put("score", new Integer(200));
  object.put("current", new Double(152.32));
  object.put("nickname", "Hacker");
 } catch (JSONException e) {
  e.printStackTrace();
 }
 System.out.println(object);
}

希望本文所述对大家的Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务