android app无法从servlet接收InputStream

时间:2014-04-29 07:51:49

标签: android servlets

我想在新的Activity中使用servlet发送的字符串。 servlet运行正常,但android app中有很多错误。

单击“提交”按钮时运行。 我正在使用Tomacatv5.5和Eclipse IDE。我是否需要导入服务器的任何库或什么???我无法理解这个问题。

这是完整的logcat:

  

04-29 05:05:08.517:D /(1035):HostConnection :: get()建立新主机连接0xb75923d0,tid 1035

04-29 05:05:08.707:W / EGL_emulation(1035):eglSurfaceAttrib未实现

04-29 05:05:08.737:D / OpenGLRenderer(1035):启用调试模式0

04-29 05:05:16.157:D / InputEventConsistencyVerifier(1035):KeyEvent:ACTION_UP但键未关闭。

04-29 05:05:16.157:D / InputEventConsistencyVerifier(1035):在android.widget.EditText {b4d2f200 VFED..CL .F ...... 115,173-228,203#7f080003 app:id / editText2}

04-29 05:05:16.157:D / InputEventConsistencyVerifier(1035):0:发送于327856000000, KeyEvent {action = ACTION_UP,keyCode = KEYCODE_TAB,scanCode = 15,metaState = 0,flags = 0x8,repeatCount = 0,eventTime = 327856,downTime = 327822,deviceId = 0,source = 0x101}

04-29 05:05:22.557:D / O冲洗(1035):你好哟

04-29 05:05:22.717:D / dalvikvm(1035):GC_FOR_ALLOC释放206K,9%免费2981K / 3264K,暂停77ms,总计88ms

04-29 05:05:22.757:D / AndroidRuntime(1035):关闭虚拟机

04-29 05:05:22.767:W / dalvikvm(1035):threadid = 1:线程退出未捕获的异常(group = 0xb4a64ba8)

04-29 05:05:22.817:E / AndroidRuntime(1035):致命异常:主

04-29 05:05:22.817:E / AndroidRuntime(1035):进程:com.example.mticket,PID:1035

04-29 05:05:22.817:E / AndroidRuntime(1035):java.lang.NullPointerException:println需要一条消息

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.util.Log.println_native(原生方法)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.util.Log.d(Log.java:139)

04-29 05:05:22.817:E / AndroidRuntime(1035):at com.example.mticket.MainActivity $ 3.onClick(MainActivity.java:232)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.view.View.performClick(View.java:4438)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.view.View $ PerformClick.run(View.java:18422)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.os.Handler.handleCallback(Handler.java:733)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.os.Handler.dispatchMessage(Handler.java:95)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.os.Looper.loop(Looper.java:136)

04-29 05:05:22.817:E / AndroidRuntime(1035):在android.app.ActivityThread.main(ActivityThread.java:5017)

04-29 05:05:22.817:E / AndroidRuntime(1035):at java.lang.reflect.Method.invokeNative(Native Method)

04-29 05:05:22.817:E / AndroidRuntime(1035):at java.lang.reflect.Method.invoke(Method.java:515)

04-29 05:05:22.817:E / AndroidRuntime(1035):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)

04-29 05:05:22.817:E / AndroidRuntime(1035):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-29 05:05:22.817:E / AndroidRuntime(1035):at dalvik.system.NativeStart.main(Native Method)

04-29 05:05:27.607:I / Process(1035):发送信号。 PID:1035 SIG:9

请帮助,我真的被卡住了。

这是我的Android应用程序代码:

submit.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        try
        {
URL url = new URL("http://localhost:8080/Mobticket/ServletLogin");
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "text/html");
            Log.d("Os flush", "yo yo");
            OutputStream os = conn.getOutputStream();
            os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {             throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

             Toast.makeText(MainActivity.this, "Message"+br.readLine(),Toast.LENGTH_LONG).show();

            String output;

            while ((output = br.readLine()) != null) {
                Log.d("Servlet", output);
            }

            if(output.length()>0)
            {
    Intent intent = new Intent(getApplicationContext(),MainMenu.class);
                Bundle b=new Bundle();

                b.putString("user",userid.getText().toString() );


                intent.putExtras(b);
                startActivity(intent);

                conn.disconnect();
                os.close();
                br.close();
                finish();
            }





        }
        catch(Exception e)
        {
            Log.d("Error",e.getMessage());}
            conn.disconnect();
        }




});

0 个答案:

没有答案