我很難確定我的代碼中存在什麼問題。我被困在這個部分,調試器有點神祕。 請參閱隨附代碼: 清單文件有http連接中的調試問題
<uses-permission android:name="android.permission.INTERNET"/>
下一步是開始時的代碼,我已經打上了System.out的「異常1」和「異常2兩層的try/catch異常「到quicky在logcat的
package com.example.httpexample;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class HttpExample extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
System.out.println("Exception 1");
e.printStackTrace();
}
}
}
代碼的第二塊是實際的HTTP類識別
package com.example.httpexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
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;
public class GetMethodEx {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://google.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null){
try{
in.close();
return data;
} catch (Exception e) {
System.out.println("Exception 2");
e.printStackTrace();
}
}
}
}
}
我的問題是,我無法確定我在錯誤的地方看到使用調試器的問題是什麼,那裏有更多的信息?這是我在異常之後得到的一小部分logcat輸出,但是我不知道是否需要解釋它,或者是否有更多的信息在其他地方,請提前致謝!
07-19 21:37:30.916: I/System.out(4987): Exception 1
07-19 21:37:30.916: W/System.err(4987): android.os.NetworkOnMainThreadException
07-19 21:37:30.936: W/System.err(4987): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-19 21:37:30.936: W/System.err(4987): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-19 21:37:30.936: W/System.err(4987): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-19 21:37:30.947: W/System.err(4987): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-19 21:37:30.947: W/System.err(4987): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-19 21:37:30.956: W/System.err(4987): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-19 21:37:30.966: W/System.err(4987): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-19 21:37:30.966: W/System.err(4987): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-19 21:37:30.966: W/System.err(4987): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-19 21:37:30.976: W/System.err(4987): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-19 21:37:30.976: W/System.err(4987): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-19 21:37:30.986: W/System.err(4987): at com.example.httpexample.GetMethodEx.getInternetData(GetMethodEx.java:22)
07-19 21:37:30.986: W/System.err(4987): at com.example.httpexample.HttpExample.onCreate(HttpExample.java:21)
07-19 21:37:30.996: W/System.err(4987): at android.app.Activity.performCreate(Activity.java:5104)
07-19 21:37:30.996: W/System.err(4987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
..
..
.. and more
你說得對,它與StrictMode的關係迫使你不要使用主線程來執行額外的任務,謝謝。 – user2566468