Android 解析服务器JSON对象
2011-10-06 16:45
375 查看
view
plain
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;
}
}
}
plain
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;
}
}
}
相关文章推荐
- android的从服务器取得json对象进行解析
- android的从服务器取得json对象进行解析
- Android数据格式解析对象JSON用法
- Android数据格式解析对象JSON用法
- App下载更新、数据库断点续传、通知栏更新下载进度、Https传服务器Json并且解析对象
- Android数据格式解析对象JSON用法
- Android JSON 数据里面 嵌套了JSONObject对象,怎么解析成Bean实体?
- Android数据格式解析对象JSON用法
- ios 接受 java 服务器 框架 spring mvc 解析json对象 存在中文乱码 已解决!!!!!
- Android如何解析json数组对象
- Android数据格式解析对象JSON用法(转)
- android解析xml和json数据(获取本地服务器数据)
- Android如何解析json数组对象
- android 用JSON去服务器获取JSON对象 和其他的几种方法去服务器获取JSON对象
- [Android | 小代码] 天气预报 简易Demo json解析数组+对象 Gson+Volley简单使用
- Android 使用GSON解析JSON 并转换为JAVA对象
- Android解析json数组对象
- android json解析对象
- android 客户端访问自己建立的服务器并返回JSON数据进行解析学习
- Android与服务器间数据传递及JSON解析