从 Android 客户端连接到 servlet 时出现问题

发布于 2024-10-25 07:01:20 字数 3532 浏览 3 评论 0原文

我正在尝试连接到 Servlet(Tomcat localhost)。这是我的代码。

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 打印的任何内容。这是如何引起的以及如何解决?

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

ServletTest.java (the client)

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 (the server)

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);
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-11-01 07:01:20

您没有执行 HTTP 请求。正如您所想的那样,在写入请求正文并关闭流后,URLConnection 不会执行此操作。仅当您从响应中读取任何内容时,它才会执行。

写入请求正文之后执行此操作。例如获取响应状态和/或正文。

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();
    // ...
}

另请参阅:


此外,在 Android 上,您还需要设置用户权限来连接互联网。将以下内容添加到 AndroidManiext.xml 中。

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

另请参阅:

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();
    // ...
}

See also:


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" />

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文