后httpclient.execute(HTTPGET)的Andr​​oid code没有得到在试运行(使用的AsyncTask) [英] Android code after httpclient.execute(httpget) doesn't get run in try (using AsyncTask)

查看:122
本文介绍了后httpclient.execute(HTTPGET)的Andr​​oid code没有得到在试运行(使用的AsyncTask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网站获取数据并解析到我的Andr​​oid应用程序。 不幸的是我甚至不得到解析数据的一部分。 以下行后,code不运行:

 的Htt presponse响应= httpclient.execute(HTTPGET);
 

其结果是,邮件= 333。 当我使用Eclipse调试跨过它,我可以看到,它运行的线路。然后,我可以读出响应变量(= 200),但在这之后是不执行的行。 它没有达到444或其他code中的功能。 我已经宣布了Android清单上网权限。

在code:

 进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口org.apache.http.Htt presponse;
进口org.apache.http.StatusLine;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口android.os.AsyncTask;
进口android.widget.TextView;

公共类MainActivity延伸活动{

TextView的消息;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    消息=(TextView中)findViewById(R.id.message);

    message.setText(111);
    //新getinternetData .execute();

}


@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}


@覆盖
保护无效的OnStart(){
    super.onStart();
    新getinternetData()执行();
    //message.setText("OnStart);
}

私有类getinternetData扩展的AsyncTask<字符串,太虚,字符串> {

    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        HttpClient的HttpClient的=新DefaultHttpClient();
        HTTPGET HTTPGET =新HTTPGET(http://www.google.com);
        尝试{
            message.setText(333);
            HTT presponse响应= httpclient.execute(HTTPGET);
            message.setText(444);
            状态行状态行= response.getStatusLine();
            INT状态code = statusLine.getStatus code();
            如果(状态code == 200){
                message.setText(可以连接);
            }其他
                message.setText(无法​​连接);
        }赶上(例外五){
            message.setText(e.toString());
        }

        message.setText(555);
        返回null;
    }

    @覆盖
    保护无效onPostExecute(字符串结果){
        //message.setText("Postsequence);
    }
}
 

我不知道我做错了。
是不是因为我没有使用的AsyncTask是否正确?
或者,也许不使用httpclient.execute是否正确?

编辑: 谢谢您的帮助 我已经改变了消息log.d,现在我可以看到,我可以连接。 该故障是在更新屏幕和这样的。我有很多东西要学。

下面是我改变code部分:

 私有静态最后字符串变量=MainActivity;

    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        HttpClient的HttpClient的=新DefaultHttpClient();
        HTTPGET HTTPGET =新HTTPGET(http://www.google.com);
        尝试{
            //message.setText("333);
            Log.d(TAG,以前HTT presponse);
            HTT presponse响应= httpclient.execute(HTTPGET);
            Log.d(TAG,经过HTT presponse);
            状态行状态行= response.getStatusLine();
            INT状态code = statusLine.getStatus code();
            Log.d(TAG,之后的状态code,code =+状态code);
            如果(状态code == 200){
                Log.d(TAG,可以连接);
            }其他{
                Log.d(TAG,无法连接);
            }
        }赶上(例外五){
            Log.d(TAG,e.toString());
        }
        Log.d(TAG,退出做后台);
        返回null;
 

logcat的结果:

 :D / MainActivity(22323):在HTT presponse
 一堆code
 :D / MainActivity(22323):在HTT presponse
 :D / MainActivity(22323):后状态code,code = 200
 :D / MainActivity(22323):可以连接
 :D / MainActivity(22323):退出做后台
 

解决方案

一个问题,你必须是你正在试图更新从后台线程屏幕(消息)。使用AsyncTask.publishProgress(...)让主线程更新其onProgressUpdate()方法的图形用户界面。

此外,而不是更新屏幕,考虑写日志消息Log.d(TAG,你在这里。);看他们在logcat中

这并没有解决HTTP问题,但清除这件事首先要确保你没有造成挂通过改变屏幕在后台线程。

I'm trying to get data from a website and parse it into my android application. Unfortunately I don't even get to the part of parsing the data. The code doesn't run after the following line:

HttpResponse response = httpclient.execute(httpget);

The result is that message = 333. When I step over it using the eclipse debugger, I can see that it runs that line. Then I can read out the response variable (=200) but the lines after that is doesn't execute. It doesn't reach the 444 or other code in the function. I have declared the internet permission in the Android manifest.

The Code:

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

I don't know what I'm doing wrong.
Is it because I'm not using AsyncTask properly?
Or maybe not using the httpclient.execute properly?

EDIT: Thanks for the help I've changed the message to log.d and now I can see that I can connect. The fault is in updating the screen and such. I've got much to learn.

Here is my changed code part:

    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 result:

 : 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

解决方案

One problem you have is you are attempting to update the screen (message) from a background thread. Use AsyncTask.publishProgress(...) to let the main thread update the GUI in its onProgressUpdate() method.

Also rather than updating the screen, consider writing log messages Log.d(TAG, "You Are Here.); and watching them in logcat.

This doesn't address the HTTP issue, but clear this up first to be sure you are not causing a hang by changing the screen in a background thread.

这篇关于后httpclient.execute(HTTPGET)的Andr​​oid code没有得到在试运行(使用的AsyncTask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆