我很难确定我的代码中的问题所在。我被困在这部分,调试器有点神秘。请参阅随附的代码:清单文件具有
<uses-permission android:name="android.permission.INTERNET"/>
接下来是开始的代码,我用 system.out "Exception 1" 和 "Exception 2" 标记了两个 try/catch 异常,以便在 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