2012-02-07 63 views
1

编辑:我只是将我的HTTPRequest代码移动到onCreate,现在突然它工作!有人可以告诉我为什么从点击按钮调用它时不起作用吗? **HttpRequest崩溃在按钮点击

我使用HTTP请求来读取某些信息,但它不断崩溃。

完全相同的HttpRequest方法在我的应用中的另一个类中工作。

它似乎是当我试图分配字符串响应的东西,在这种情况下,一个文本框,它超时?

这是包含的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 + "&param1=" + p1; 
       geturl = geturl + "&param2=" + 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); 

    } 

这是我的日志文件的一部分,我相信包含相关消息:

02-07 19:05:09.636 E/MyApp :: (23414): URI: http://www.mysite.com/send.aspx?code=rfpaefsJIOLMNSBFep&param1=Gggg&param2=Bnn 
02-07 19:05:09.636 E/MyApp :: (23414): URI Params: [email protected] 
02-07 19:05:09.644 E/MyApp :: (23414): URI Request Line: GET http://www.MySite.com/send.aspx?code=sdfgkiukjehgefsJIOLMNSBFep&param1=Gggg&param2=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} 
+0

该日志不包含你所需要的。尝试在DDMS中添加过滤器以仅显示来自应用的日志。 – zrgiu 2012-02-07 04:11:56

+0

其实我经​​常不使用DDMS,在我的手机上安装软件包要快得多。但无论如何我更新了上面的日志文件,我看到的唯一的东西是2行,其中包括'地址族不受协议支持'。为什么?它适用于应用程序中的其他活动。 – Dave 2012-02-07 14:39:49

+0

就像一个完整的检查,你可以在实例化它后检查URI - 你当前正在记录geturl - 但它是否正确解析(不是我能明白它为什么不应该) – Basic 2012-02-07 15:14:09

回答

1

我相信这个问题可能是结果糟糕的设计。我将HTTPRequest放在按钮单击中,这在点击过程中引起了明显的延迟。可怕的用户界面设计,但我认为这是可以的“初稿”和测试。

我怀疑我的手机因为这个延迟而崩溃,操作系统预计对按钮点击的响应速度会更快。我使用自定义ROM,也许这不会发生在其他手机上。在Log或我的代码中也没有什么线索,因为技术上代码是好的。

无论如何,在使用AsyncTask将HTTPRequest放入其自己的线程之后,它非常棒。