JSONObject解析

import java.io.IOException;
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.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

private String path="http://japi.juhe.cn/joke/content/list.from?key=%20874ed931559ba07aade103eee279bb37%20&page=1&pagesize=10&sort=asc&time=1418745237";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


            new Thread(){
                public void run() {
                    getdata();
                }

            }.start();


}

//请求数据
private void getdata() {
// TODO Auto-generated method stub
// /得到HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 得到连接方式对象,设置请求路径
HttpGet httpGet = new HttpGet(path);

            try {
                // GET方式请求接口,得到响应内容
                HttpResponse httpResponse = httpClient.execute(httpGet);

                // 得到状态行
                StatusLine statusLine = httpResponse.getStatusLine();
                // 得到状态码
                int statusCode = statusLine.getStatusCode();
                // 如果状态码等于200,代表请求成功
                if (statusCode == 200) {
                    // 得到包含着数据的实体
                    HttpEntity entity = httpResponse.getEntity();
                    // 讲实体转换成String,并设置编码集
                    String data1 = EntityUtils.toString(entity,"gbk");
                     System.out.println(data1);

                     try {
                        JSONObject dataJson=new JSONObject(data1);
                         JSONObject result=dataJson.getJSONObject("result");
                         JSONArray data=result.getJSONArray("data");
                         for (int i = 0; i < 10; i++) {
                             JSONObject info=data.getJSONObject(i);
                             String content=info.getString("content");
                             String hashId=info.getString("hashId");
                             String unixtime=info.getString("unixtime");
                             String updatetime=info.getString("updatetime");

                             System.out.println(content+"  "+hashId+"  "+unixtime+"  "+updatetime); 
                        }

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                } 

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

}

你可能感兴趣的:(JSONObject解析)