http连接中的调试问题
问题描述:
我很难确定我的代码中存在什么问题。我被困在这个部分,调试器有点神秘。 请参阅随附代码: 清单文件有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
答
Here is an article关于您遇到的异常。
您还可以将Log.d(字符串,字符串)行添加到您的代码中,以遵循每一步并准确查看卡住的位置。第一个参数是标签,第二个参数是一个字符串,您可以定义哪一个LogCat会在您点击该行代码时向您吐出。
这非常有帮助。我敢打赌,文章有你的答案。
编辑添加developer doc,看起来像在你的主线程中运行Network动作是罪魁祸首。
编辑#2用于修复:TechBlogIsTech Article
编辑#3以获得更多关于从的AsyncTask developer docs。
最终编辑更多关于LogCat debug log writing。
你说得对,它与StrictMode的关系迫使你不要使用主线程来执行额外的任务,谢谢。 – user2566468