package com.yxy;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.JSONObject;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class HelloActivity extends Activity {
/** Called when the activity is first created. */
private TextView showStr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient client = new DefaultHttpClient();
StringBuilder sb = new StringBuilder();
String mystring = "";
HttpGet myget = new HttpGet("http://localhost/");
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
for(String s = reader.readLine(); s != null; s = reader.readLine()) {
sb.append(s);
}
JSONArray ja = new JSONArray(sb.toString());
for(int i = 0; i <= ja.length()-1; i++) {
JSONObject jsonObject = ja.getJSONObject(i);
mystring += "第" + i + "个id:" + jsonObject.getInt("title")
+ "";
}
showStr.setText(mystring);
} catch (Exception e) {
// TODO: handle exception
showStr.setText(e.toString());
}
}
}程序一运行,就报错误啊。程序运行中止。我这是哪里写得有问题啊?望高手解答。
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.JSONObject;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class HelloActivity extends Activity {
/** Called when the activity is first created. */
private TextView showStr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient client = new DefaultHttpClient();
StringBuilder sb = new StringBuilder();
String mystring = "";
HttpGet myget = new HttpGet("http://localhost/");
try {
HttpResponse response = client.execute(myget);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
for(String s = reader.readLine(); s != null; s = reader.readLine()) {
sb.append(s);
}
JSONArray ja = new JSONArray(sb.toString());
for(int i = 0; i <= ja.length()-1; i++) {
JSONObject jsonObject = ja.getJSONObject(i);
mystring += "第" + i + "个id:" + jsonObject.getInt("title")
+ "";
}
showStr.setText(mystring);
} catch (Exception e) {
// TODO: handle exception
showStr.setText(e.toString());
}
}
}程序一运行,就报错误啊。程序运行中止。我这是哪里写得有问题啊?望高手解答。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货