Edit: I just moved my HTTPRequest code into the onCreate, and now all of a sudden it works! Can someone tell me why it does not work when called from the button click? **
I’m using HTTP request to read in some info, but it keeps crashing.
The exact same HttpRequest method works in another class in my app.
It seems to be when I try to assign the String response to something, in this case a TextBox, it times out?
This is the code that contains the HttpRequest:
public class InfoScreen extends Activity
{
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.Infoscreen);
}
public void InfoClicked(View target) throws Exception {
TextView t1 = (TextView)findViewById(R.id.tp1);
String p1 = "";
p1 = t1.getText().toString();
TextView t2 = (TextView)findViewById(R.id.tp2);
String p2 = "";
p2 = t2.getText().toString();
String serverresponse="";
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
String geturl = "http://www.MySite.com/send.aspx?code=rfpaefsJIOLMNSBFep";
geturl = geturl + "¶m1=" + p1;
geturl = geturl + "¶m2=" + p2;
request.setURI(new URI(geturl));
Log.e("MyApp :: ", "URI: " + request.getURI());
Log.e("MyApp :: ", "URI Params: " + request.getParams());
Log.e("MyApp :: ", "URI Request Line: " + request.getRequestLine());
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
serverresponse = in.readLine();
in.close();
}
catch (Exception e) {
Log.e("MyApp :: ", "Error getting Info using HTTP");
e.printStackTrace();
}
TextView t3 = (TextView)findViewById(R.id.tInfoStatus);
t3.setText(serverresponse);
}
Here is part of my log file which I believe contain relevant messages:
02-07 19:05:09.636 E/MyApp :: (23414): URI: http://www.mysite.com/send.aspx?code=rfpaefsJIOLMNSBFep¶m1=Gggg¶m2=Bnn
02-07 19:05:09.636 E/MyApp :: (23414): URI Params: org.apache.http.params.BasicHttpParams@4010bac8
02-07 19:05:09.644 E/MyApp :: (23414): URI Request Line: GET http://www.MySite.com/send.aspx?code=sdfgkiukjehgefsJIOLMNSBFep¶m1=Gggg¶m2=Bnn HTTP/1.1
02-07 19:05:13.023 I/ActivityManager( 107): Start proc com.keramidas.TitaniumBackup for broadcast com.keramidas.TitaniumBackup/.PackageEventsReceiver: pid=23520 uid=10112 gids={1015, 3003}
02-07 19:05:13.027 D/AndroidRuntime(23414): Shutting down VM
02-07 19:05:13.027 W/dalvikvm(23414): threadid=1: thread exiting with uncaught exception (group=0x40015578)
02-07 19:05:13.042 E/AndroidRuntime(23414): FATAL EXCEPTION: main
02-07 19:05:13.042 E/AndroidRuntime(23414): java.lang.IllegalStateException: Could not execute method of the activity
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.view.View$1.onClick(View.java:2154)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.view.View.performClick(View.java:2538)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.view.View$PerformClick.run(View.java:9152)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.os.Handler.handleCallback(Handler.java:587)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.os.Looper.loop(Looper.java:130)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.app.ActivityThread.main(ActivityThread.java:3688)
02-07 19:05:13.042 E/AndroidRuntime(23414): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 19:05:13.042 E/AndroidRuntime(23414): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 19:05:13.042 E/AndroidRuntime(23414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-07 19:05:13.042 E/AndroidRuntime(23414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-07 19:05:13.042 E/AndroidRuntime(23414): at dalvik.system.NativeStart.main(Native Method)
02-07 19:05:13.042 E/AndroidRuntime(23414): Caused by: java.lang.reflect.InvocationTargetException
02-07 19:05:13.042 E/AndroidRuntime(23414): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 19:05:13.042 E/AndroidRuntime(23414): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 19:05:13.042 E/AndroidRuntime(23414): at android.view.View$1.onClick(View.java:2149)
02-07 19:05:13.042 E/AndroidRuntime(23414): ... 11 more
02-07 19:05:13.042 E/AndroidRuntime(23414): Caused by: java.lang.NullPointerException
02-07 19:05:13.042 E/AndroidRuntime(23414): at com.MyApp.lmanywhere.InfoScreen.InfoClicked(InfoScreen.java:77)
02-07 19:05:13.042 E/AndroidRuntime(23414): ... 14 more
02-07 19:05:13.054 W/ActivityManager( 107): Force finishing activity com.MyApp.lmanywhere/.InfoScreen
02-07 19:05:13.058 D/InputDispatcher( 107): findTouchedWindow finished: injectionResult=-1, injectionPermission=1, timeSpentWaitingForApplication=1587.8ms
02-07 19:05:13.058 D/InputDispatcher( 107): findTouchedWindow finished: injectionResult=-1, injectionPermission=1, timeSpentWaitingForApplication=1589.5ms
02-07 19:05:13.074 E/ ( 107): Dumpstate > /data/log/dumpstate_app_error
02-07 19:05:13.148 I/dumpstate(23526): begin
02-07 19:05:13.496 I/StatusBarPolicy( 184): onDataActivity-D:3
02-07 19:05:13.562 W/ActivityManager( 107): Activity pause timeout for HistoryRecord{4094b440 com.MyApp.lmanywhere/.InfoScreen}
I believe this problem might be the result of bad design on my part. I placed the HTTPRequest inside a button click, which caused a noticeable delay during the click. Horrible UI design, but I thought it was ok for a “first draft” and for testing.
I suspect my phone was crashing due to this delay, that the OS expected a faster response to a button click. I’m using a custom ROM and maybe this wouldn’t happen on other phones. There would also be little clue in the Log or in my Code, since technically the code was fine.
At any rate, after putting the HTTPRequest inside its own thread using AsyncTask it works great.