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