我是新來的Android和新的Stackoverflow!HttpResponse/httpClient.execute錯誤
我只是創建虛擬應用程序來了解http連接。 在編譯我在此行
的HttpResponse getbackdata = http_client.execute(url_data)遇到一個錯誤;
我甚至在stackoverflow中搜索,但大多數人出來建議使用異常處理來捕獲UnknownHostException。我做到了。我不知道我在哪裏搞亂了代碼。 這可能是一個小錯誤,因爲我是初學者,我將從中學習。 在此先感謝。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
HttpClient http_client=new DefaultHttpClient();
URI url=new URI("http://www.mysite.com");
HttpGet url_data=new HttpGet(url);
HttpResponse getbackdata= http_client.execute(url_data);
InputStreamReader in =new InputStreamReader(getbackdata.getEntity().getContent());
BufferedReader br = new BufferedReader(in);
StringBuffer sb=new StringBuffer("");
String info="";
String nl=System.getProperty("line.separator");
while((info=br.readLine())!=null){
sb.append(info.toString()+nl);
}
br.close();
TextView output=(TextView) findViewById(R.id.display_output);
output.setText(sb.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.d("ARR_ERROR","UnknownHostException : "+e);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.d("ARR_ERROR","ClientProtocolException : "+e);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("ARR_ERROR","IOException : "+e);
}
catch (URISyntaxException e) {
// TODO Auto-generated catch block
Log.d("ARR_ERROR","URISyntaxException : "+e);
}
}
使用異步類。發佈logcat。 –
我想知道爲什麼嚇倒人們downvote我的問題! –