Android连接servlet的简单例子

时间:2015-11-18 08:34:09

标签: java android servlets

我知道存在这个问题,但在运行程序时我确实找不到任何问题。我试着收到“Hello Android !!!”来自servlet的文本,并使用Log.i()显示它。

注意我使用的是Android Studio和java servlet。

这将在button.onClick()时触发,我尝试了方法1和方法2。

方法1:

            try {
                InputStream stream = null;

                URL url = new URL ("http://xxx/HelloWorldServlet");
                URLConnection connection = url.openConnection();

                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestProperty("loginId", mUsername.getText().toString());
                connection.setRequestProperty("password", mPassword.getText().toString());

                try {

                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    httpConnection.setRequestMethod("GET");
                    httpConnection.connect();

                    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        stream = httpConnection.getInputStream();
                    }



                    BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
                    String s = "";
                    while ((s = buffer.readLine()) != null)
                    {Log.i(TAG, s);}

                    ((HttpURLConnection) connection).disconnect();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();

            }         
        }

方法2:

InputStream is = null;

            try {
                URL url = new URL("http://xxx/xxx/mobileapps/HelloWorldServlet");

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                // Starts the query
                conn.connect();

                int response = conn.getResponseCode();
                Log.d(TAG, "The response is: " + response);
                is = conn.getInputStream();

                String contentAsString = is.toString() ;
                conn.disconnect();

                Log.i(TAG, contentAsString);
            }catch (Exception e) {}

Servlet代码:

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
     * Servlet implementation class HelloWorldServlet
     */
    @WebServlet("/HelloWorldServlet")
    public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HelloWorldServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         HttpServletResponse response) throws ServletException, IOException {
                PrintWriter out = response.getWriter();
                out.println("Hello Android !!!!");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

This is what I get after button triggered

1 个答案:

答案 0 :(得分:0)

看看这个:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
你必须在一个线程中进行联网,而不是像你那样。