http连接中的调试问题

时间:2013-07-19 21:59:39

标签: android

我很难确定代码中的问题。我被困在这一部分,调试器有点神秘。 请参阅随附的代码:

清单文件
<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

1 个答案:

答案 0 :(得分:0)

Here is an article关于您获得的例外情况。

您还可以将Log.d(String,String)行添加到代码中,以执行每个步骤并准确查看卡住的位置。第一个参数是标记,第二个参数是您定义的字符串,当LogCat到达该行代码时,它将向您吐出。

非常有帮助。我敢打赌这篇文章有你的答案。

修改以添加developer doc,看起来在主线程中运行网络操作是罪魁祸首。

修补程序

编辑#2 TechBlogIsTech Article

编辑#3 了解有关来自developer docs的AsyncTask的更多信息。

最终修改了解LogCat debug log writing上的更多内容。