3

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
user2192871
  • 33
  • 1
  • 4
  • Sounds more like a problem with your text view. Does the code pause at any time, or you can actually step through to the end of the method? – Perception Mar 20 '13 at 22:42

2 Answers2

1

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.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Thanks! Because of the logcat I found out the http code is correct. I'll check up more on how to work with the aSyncTask. – user2192871 Mar 20 '13 at 23:30
0

Alike Dale Wilson said, you can't update UI in doInBackground method. Prefer onPreExecute and onPostExecute methods.

You can also update your message TextView with the result of your doInBackground like that :

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);
    }   
}
ludriv
  • 291
  • 1
  • 6
  • 15