所以我有一个问题,为什么我的函数getIt()
会中断httpGet
并给出Exception
,在清单中我添加了INTERNET
权限!我没有使用 AsyncTask,因为我需要创建一个新类,如果我用它调用 AsyncTask,get
它会阻塞我的 UI
类.java
public class loadMovies{
public String getIt(){
InputStream ips = null;
JSONObject json = null;
String get = "";
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://mysite/app/movies");
try {
HttpResponse response = client.execute(httpGet);
ips = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(ips));
StringBuilder builder = new StringBuilder();
String line;
while((line = buffer.readLine()) != null){
builder.append(line+"\n");
}
json = new JSONObject(builder.toString());
Log.d(TAG, json.toString());
}
catch(Exception e){ e.printStackTrace(); }
if(json != null){
JSONArray array = json.optJSONObject("data").optJSONArray("movies");
for(int i = 0; i < array.length(); i++){
JSONObject rss = array.optJSONObject(i);
get += rss.optString("name")+"\n";
}
}
return get;
}
}