簡體   English   中英

HTTP連接中的調試問題

[英]Debugging issues in http connection

我很難識別代碼中的問題。 我被困在這部分上,調試器某種程度上是神秘的。 請參閱隨附的代碼:清單文件具有

<uses-permission android:name="android.permission.INTERNET"/>

接下來是開始的代碼,我已經用system.out標記了兩個try / catch異常,在“ logcat”中可以快速識別“ Exception 1”和“ Exception 2”

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

這是有關您遇到的異常的文章

您還可以在代碼中添加Log.d(String,String)行,以執行每個步驟,並確切地了解卡住的位置。 第一個參數是標簽,第二個參數是您定義的字符串,當LogCat遇到該行代碼時,它將向您吐口水。

這非常有幫助。 我敢打賭,這篇文章有您的答案。

編輯以添加開發人員doc ,這似乎是在主線程中運行Network動作的罪魁禍首。

編輯#2進行修復: TechBlogIsTech文章

編輯#3以獲得更多來自開發人員文檔的有關AsyncTask的信息。

有關LogCat調試日志編寫的更多信息,請進行最終編輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM