3

我正在尝试从网站获取数据并将其解析到我的 android 应用程序中。不幸的是,我什至没有进入解析数据的部分。在以下行之后代码不会运行:

HttpResponse response = httpclient.execute(httpget);

结果是 message = 333。当我使用 eclipse 调试器跳过它时,我可以看到它运行了该行。然后我可以读出响应变量(= 200),但之后的行不执行。它没有到达函数中的 444 或其他代码。我已经在 Android 清单中声明了互联网权限。

编码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    message = (TextView)findViewById(R.id.message);

    message.setText("111");
    //new getinternetData .execute("");

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
protected void onStart(){
    super.onStart();
    new getinternetData().execute("");
    //message.setText("OnStart");
}

private class getinternetData extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.google.com");
        try{
            message.setText("333");
            HttpResponse response = httpclient.execute(httpget);
            message.setText("444");
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if(statusCode == 200){
                message.setText("Could connect");
            }else
                message.setText("Couldn't connect");
        }catch(Exception e){
            message.setText(e.toString());
        }

        message.setText("555");
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        //message.setText("Postsequence");
    }   
}

我不知道我做错了什么。
是因为我没有正确使用 AsyncTask 吗?
或者可能没有正确使用 httpclient.execute?

编辑:感谢您的帮助,我已将消息更改为 log.d,现在我可以看到我可以连接。故障在于更新屏幕等。我有很多东西要学。

这是我更改的代码部分:

    private static final String TAG = "MainActivity";

    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.google.com");
        try{
            //message.setText("333");
            Log.d(TAG, "Before httpResponse");
            HttpResponse response = httpclient.execute(httpget);
            Log.d(TAG, "After httpResponse");
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            Log.d(TAG, "after statusCode, code= " +statusCode);
            if(statusCode == 200){
                Log.d(TAG, "Could connect");
            }else{
                Log.d(TAG, "Couldn't connect");
            }
        }catch(Exception e){
            Log.d(TAG, e.toString());
        }
        Log.d(TAG, "Exit do in background");
        return null;

日志猫结果:

 : D/MainActivity(22323): Before httpResponse
 bunch of code
 : D/MainActivity(22323): After httpResponse
 : D/MainActivity(22323): after statusCode, code= 200
 : D/MainActivity(22323): Could connect
 : D/MainActivity(22323): Exit do in background
4

2 回答 2

1

您遇到的一个问题是您试图从后台线程更新屏幕(消息)。使用 AsyncTask.publishProgress(...) 让主线程在其 onProgressUpdate() 方法中更新 GUI。

另外,不要更新屏幕,而是考虑编写日志消息 Log.d(TAG, "You Are Here.); 并在 logcat 中观察它们。

这并没有解决 HTTP 问题,但首先清除它以确保您不会通过在后台线程中更改屏幕而导致挂起。

于 2013-03-20T22:48:51.657 回答
0

就像 Dale Wilson 所说,你不能在 doInBackground 方法中更新 UI。更喜欢 onPreExecute 和 onPostExecute 方法。

您还可以使用 doInBackground 的结果更新您的消息 TextView ,如下所示:

private class getinternetData extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://www.google.com");
        try{
            //message.setText("333");
            HttpResponse response = httpclient.execute(httpget);
            //message.setText("444");
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if(statusCode == 200){
                return "Could connect";
            }else
                return "Couldn't connect";
        }catch(Exception e){
            return e.toString();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null)
            message.setText(result);
    }   
}
于 2013-03-20T23:08:04.017 回答