Android code after httpclient.execute(httpget) doesn't get run in try (using AsyncTask) -
I am trying to get data from a website and parsing it in my Android application. Unfortunately I do not get the part of parsing the data. The code has not run after the following line:
HTTPRSpace response = httpclient.execute (httpget); The result is that message = 333. When I move it forward using the eclipse debugger, I can see that it runs that line. Then I can read the response variable (= 200), but the line after that is not executed. It does not reach 444 or other code in the ceremony. I have announced the Internet permission in the Android Manifest. Code:
Import android.os.Bundle; Import android App 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 classroom increases main activity activity (text view message; @ override protected creature to zero) (bundle saved instenstate) {super.ncre (savedinstenstate); setContentView (R.layout.activity_main); message = (textview) find VVBIID (RID), message.setText ("111"); // New OutNetData. Execute ("");} @ Override Public Boolean On Crate Option Menu (Menu Menu) {// Increase Menu; Items in the Action Bar If it exists, getMenuInflater () .flupo (R. menu.men, menu); return true;} @ override protected void onStart () {super.onstart (); new getinternetData (). Executed (""); //message.setText("OnStart ");} Private class getinternetData encrypted Task & lt; string, zero, string & gt; {@Override protected string doInBackground (string ... parameter) is expanded {http client httpclient = new DefaultHttpClient (); HttpGet httpget = New HttpGet ("http://www.google.com") Try {message.setText ("333"); HttpResponse response = httpclient.execute (httpget); Message.setText ("444"); Threaded Positions = Response .getStatusLine (); Int statusCode = statusLine.getStatusCode (); If (statusCode == 200) {message.setText ("can connect"); } Else message.setText ("Could not connect"); } Hold (exception e) {message.setText (e.toString ()); } Message.setText ("555"); Return tap; } @Override Secure Zero PostExecute (string result) {//message.setText("Postsequence "}; }} I do not know what I'm doing.
Is this because I am not using ASCII Task properly?
Or is not using httpclient.execute well?
EDIT: Thanks for the help I have changed the message to log D and now I can see that I can connect. It's such a mistake in updating the screen and I get a lot to learn.
Here is my changed code portion:
Private static final stringTAG = "main activity"; @ Override protected string doInBackground (string ... parameter) {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 ") ;;); Status = Status = response .getStatusLine (); Int statusCode = statusLine.getStatusCode (); Log D. (tag, "postcode, code =" + status code); if (statusCode == 200) {log D. (Tag can be "connect");} other {log.D. (tag "could not connect");}} hold (exception e) {log d. (Tag, estosting ());} Log D. (tag, "exit in the background"); Logging result: LogContact result:
: D / main activity (22323): Before the httpResponse bunch of code: D / main activity (22323) : After httpResponse: D / Main Activity (22323): After the status code, code = 200: D / main activity (22323): can connect: D / MainActivity (22323): Exit in the background
There is a problem that you are trying to update the screen (message) from the background thread asyncTask Use .publishProgress (...) The main thread to update the GUI in its Prprgtiapdet () method.
Also, instead of updating the screen, consider log message log D (tag, "you are here)." And they are looking into lockout.
The HTTP does not address the problem, but to make sure it is first clear that you are not changing the screen in the background thread.
Comments
Post a Comment