问题连接到的servlet从Android客户端 [英] Problem connecting to servlet From Android client

查看:168
本文介绍了问题连接到的servlet从Android客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到这个Servlet(Tomcat的本地主机)。这是我的code。

I am trying to connect to the Servlet (Tomcat localhost). Here is my code.

ServletTest.java (客户端)

public class ServletTest extends Activity {
    private static final String TAG = "Test";
        /** Called when the activity is first created. */

    protected static EditText username;
    protected static EditText password;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

         username = (EditText)findViewById(R.id.username);


         Button goButton = (Button)findViewById(R.id.connect);
         goButton.setOnClickListener(mGoListener);
    }

    private OnClickListener mGoListener = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //finish();
                String uname=username.getText().toString();
                Log.v(TAG, uname);
                networkthread ob = new networkthread(uname);

        }
    };

}

class networkthread implements Runnable {
        private static final String TAG = "Test";
        String uname;
        public networkthread(String uname) {
                this.uname =  uname;
                Thread t = new Thread(this);
                t.start();
        }
        public void run(){
            try{
                Log.v(TAG,"Inside try");
                Log.v(TAG,"before con");
                URL url=new URL("http://192.168.1.117/servletAndroid/TestAndroid");
                HttpURLConnection con;
                con=(HttpURLConnection)url.openConnection();
                Log.v(TAG,"after con");
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                con.setRequestProperty("Accept", "application/octet-stream");
                Log.v(TAG,"Before dos");
                DataOutputStream dos=new DataOutputStream(con.getOutputStream());
                Log.v(TAG,"After dos");
                dos.writeUTF(uname.trim());
                dos.flush();
                dos.close();
                Log.v(TAG,"Finish try");
            }
            catch(Exception e){
                Log.v(TAG,"Exception"+e);
            }

        }

}

TestServlet.java (服务器)

public class TestServlet extends HttpServlet {
    public void init() {
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try{
        System.out.println("test");
        DataInputStream in = new DataInputStream((InputStream) request.getInputStream());
        String uname = in.readUTF();
        //String password = in.readUTF();
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

        System.out.println(":::::::::::" + uname);


        response.setContentType("text/plain");
        //response.setContentLength(info.length());
        PrintWriter out = response.getWriter();
        out.println(uname);

        in.close();
        out.close();
        out.flush();
        }
        catch(Exception e){
            System.out.println("Exceptin "+ e);
        }

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

但我并没有得到在Tomcat的控制台由servlet打印任何东西。这是怎么造成的,我该怎么解决呢?

But I didn't got anything in Tomcat console printed by the servlet. How is this caused and how can I solve it?

推荐答案

您没有执行HTTP请求。该的URLConnection 不会这么做的写入请求主体和关闭流,你似乎认为后。它会被执行,只有当你读到的从响应任何的。

You aren't executing the HTTP request. The URLConnection won't do that after writing to the request body and closing the stream as you seem to think. It will be executed only when you read anything from the response.

在做它的 的你写请求主体。例如。得到响应状态和/或身体。

Do it after you write to the request body. E.g. get the response status and/or body.

int status = con.getResponseCode();

if (status == 200) {
    // Anything OK! Read if necessary response body. 
    // It'll contain whatever you wrote by response.getWriter() in servlet.
    InputStream response = con.getInputStream();
    // ...
}

参见:

  • <一个href="http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests">How使用的URLConnection 火和处理HTTP请求?
  • See also:

    • How to use URLConnection to fire and handle HTTP requests?
    • 此外,在Android上,你还需要设置用户权限连接互联网。添加以下到 AndroidManifext.xml

      Further, on Android you would also need to set user permission to connect the Internet. Add the following to the AndroidManifext.xml.

      <uses-permission android:name="android.permission.INTERNET" />
      

      参见:

      • Android安全和权限
      • See also:

        • Android security and permissions
        • 这篇关于问题连接到的servlet从Android客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆