HttpRequest在按钮单击时崩溃

时间:2012-02-07 02:28:06

标签: android

编辑:我刚刚将HTTPRequest代码移到onCreate中,现在突然间它可以正常工作了!有人可以告诉我为什么它从按钮点击调用时不起作用? **

我正在使用HTTP请求读取一些信息,但它一直在崩溃。

完全相同的HttpRequest方法适用于我的应用程序中的另一个类。

似乎是当我尝试将String响应分配给某个东西时,在这种情况下是TextBox,它会超时吗?

这是包含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: 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&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}

1 个答案:

答案 0 :(得分:1)

我认为这个问题可能是我设计糟糕的结果。我将HTTPRequest置于单击按钮内,这在单击期间导致明显的延迟。可怕的UI设计,但我认为它可以用于“初稿”和测试。

我怀疑我的手机由于这种延迟而崩溃,操作系统希望对按钮点击的响应更快。我正在使用自定义ROM,也许这不会发生在其他手机上。在日志或我的代码中也没有什么线索,因为从技术上讲,代码很好。

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