String readTwitterFeed = ReadJSON("http://www.ishuaji.net/android.php");
// read json
public String ReadJSON(String url){
StringBuilder builder = new StringBuilder();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
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();
}
只要执行到红色部分HttpResponse response = client.execute(httpGet),就现在运行终止,请大家帮我看一下,谢谢。
mainfest.xml中添加了<uses-permission android:name="android.permission.INTERNET"/>
------解决方案--------------------
代码本身没问题,你是不是没有放到线程中去执行,主线程不能执行这种耗时的操作,应该用异步的方法,比如
new Thread(){
@Override
public void run(){
String readTwitterFeed = ReadJSON("http://www.ishuaji.net/android.php");
}
}.start();
------解决方案--------------------
像网络操作等耗时操作,都需要另外开一个线程,不然应该会报android.os.NetworkOnMainThreadException,
你看看有没有这个错误提醒,如果有就是你把网络写在主线程里面了。