httpclient.execute(httpget)之后的Android代码无法在try中运行(使用AsyncTask)

时间:2013-03-20 22:26:06

标签: android android-asynctask httpclient

我试图从网站获取数据并将其解析为我的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;

Logcat结果:

 : 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

2 个答案:

答案 0 :(得分:1)

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

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

这不能解决HTTP问题,但首先要清除它,以确保通过更改后台线程中的屏幕来确保不会导致挂起。

答案 1 :(得分:0)

Alike 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);
    }   
}