0

I'm hoping someone can find this problem. I had an app that was fully working in server communications. Unfortunately, I somehow lost my Eclipse workspace when moving to the Windows 8 CP. I still had the .apk, and using Dex2jar and jd-gui, I was able to salvage a lot of code. I've got it all back into working condition, but this. I'm attempting to send a URL to a server, and get back a string response. Here's the code:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class login extends Activity{

<code>

public void pushLogin(View paramView){

  try{
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(loginFinal);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    errorTextView.setText(loginFinal);

    //code gets here
    String response = client.execute(request, responseHandler);
    //does not get here
    errorTextView.setText(response);
  }

My TextView always contains the string loginFinal, I cannot get it to display the response. To check this, I moved the errorTextView.setText(loginFinal); to the line after attempting to get the String response. It didn't run at that point either. I'm tearing my hair out, and I'm sure it's something simple. I've got the internet permission, I even found my original code for this portion of the app on this site as I posted it asking a separate question. This code is, as far as I can tell, identical. The only thing I can think of that changed is I moved my build target from Froyo to Honeycomb, as I decided I want to focus on tablets.

The best part is that LogCat does absolutely nothing when I press the button, triggering pushLogin. It doesn't seem to be triggering the client.execute(request, responseHandler) at all.

The Holo Dev
  • 1,016
  • 3
  • 12
  • 22
  • Why don't check the client.execute to see if it's throwing an exception. Are you sure the server is up that you're trying to contact? – dbryson Mar 06 '12 at 03:39
  • What's in the catch block after the try{}? If you say it doesn't execute the line after client.execute I have to assume that it's hitting an exception. Have you stepped through in a debugger? – Tim Mar 06 '12 at 03:39
  • I did have a catch method. I'm still incredibly new to this, but I am under the impression that this should toast some form of error. `catch (Exception e){ Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG);` I never got an error toast. Is something wrong? – The Holo Dev Mar 06 '12 at 04:26

1 Answers1

0

You are probably call pushLogin() on UI thread, Note that the thread policy has been changed since API Level 11 (HONEYCOMB), which in short, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread, otherwise you get NetworkOnMainThreadException. The correct strategy is to call pushLogin() on background thread (AsycnTask as a good example).

Hope this help.

yorkw
  • 40,926
  • 10
  • 117
  • 130
  • That was it, thanks. I plan on moving it to an asynch task. In fact, that was what I was working on when I lost my code. However, I wanted to get back to where I left off before I tried anything new. Out of curiosity, is that policy based on the build target? The reason I ask is I ran (basically) the same code, with the http communication on the UI thread on the same ICS phone and it ran fine when the target was 8 rather than 11. Again, thanks for the help! – The Holo Dev Mar 06 '12 at 04:34
  • That is why it is inside a if condition test, StrictMode.ThreadPolicy is introduced since API Level 9. condition will be checked at runtime. the code I posted works on all Android version. – yorkw Mar 06 '12 at 08:49