【问题标题】:How to send request from android to servlet with parameters如何使用参数将请求从android发送到servlet
【发布时间】:2015-11-25 15:41:33
【问题描述】:

我正在尝试从我的 android 设备向 servlet 发送带有参数的请求,并且参数作为请求的一部分发送。

我有一个点击按钮,我从 editText 视图中获取文本修改我的 url 以创建一个带有参数的 url 并将该 url 发送到 AsyncTask。

这是我的代码。

 buttonRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String stringUrl = "http://192.168.11.4:8084/WebApplication1/DemoServlet?email=" + editTextEmail.getText() + "&password=" + editTextPassword.getText() + "&phone=" + editTextPhoneNumber.getText();

            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                new RegistrationTask().execute(stringUrl);

            } else {
                Toast.makeText(RegisterActivity.this, "Error!. Please check your internnet connection", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

这是我的 RegistrationTask 代码。

class RegistrationTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        return getResponse(params[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        editTextEmail.setText(result);
    }

    public String getResponse(String myurl) {
        InputStream is = null;
        String contentAsString = null;
        int len = 500;
        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            int response = conn.getResponseCode();
            Log.d("Download", "The response is: " + response);
            is = conn.getInputStream();
            // Convert the InputStream into a string
            contentAsString = readIt(is, len);
            return contentAsString;
        } catch (Exception e) {
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return contentAsString;
    }

    // Reads an InputStream and converts it to a String.
    public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        reader.read(buffer);
        return new String(buffer);
    }

}

我尝试调试我的 servlet 代码,但请求以 email=password=phone= 的形式出现

请帮我整理一下。

【问题讨论】:

  • 看看 Apache HttpClient。对您的要求非常有用。
  • 如果您记录“stringUrl”,那么您会看到什么?请格式化您的第一个代码块,这样我们就不必水平滚动。

标签: java android mysql servlets


【解决方案1】:

不是答案,但会帮助你。

首先请也分享 servlet 代码


第二,在发送请求前使用URLEncoder.encode(arg:String):String对参数进行编码,例如如下
String stringUrl = "http://192.168.11.4:8084/WebApplication1/DemoServlet?email="
+URLEncoder.encode(editTextEmail.getText())


此外,由于您阅读信息的方式,您的方法readIt(stream:InputStream,len:int):String 也不可靠。

注意InputStream.read(buf::byte[]):int即使有太多可用数据也不会填满整个缓冲区,并且 int 响应指示写入缓冲区的数据数量,因此您的代码会出现数据不一致。
您可以使用DataInputStream.readFully(buff:byte[]):void,它要么完全填满缓冲区,要么在没有足够数据时抛出异常。
或者更好的方法读到 EOS,read() 方法返回 -1。

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多