java - 从android应用程序接收java servlet中的空值

标签 java android servlets

    public void doGet(HttpServletRequest req, HttpServletResponse resp)

        throws IOException {
              String user = req.getParameter("id");
              resp.setContentType("text/plain");
              PrintWriter writer = resp.getWriter();
              writer.write("value"+user);}}
**********************************************************************
**Android side code**
EditText numDisplay;
Button calculateNow;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    numDisplay= (EditText)findViewById(R.id.editText1);
    calculateNow = (Button)findViewById(R.id.button1);  
    calculateNow.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            {

                new MyAsyncTask().execute(numDisplay.getText().toString());     

            } }
     } );
}

    private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
        protected Double doInBackground(String... params) {

            postData(params[0]);
            return null;
        }
        protected void onPostExecute(Double result){

            Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
        }
        protected void onProgressUpdate(Integer... progress){
            //pb.setProgress(progress[0]);
        }
        public void postData(String valueIWantToSend) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://primecalculation.appspot.com/");
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("id", valueIWantToSend));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

我正在向 servlet 发送值,但在 servlet 中我得到的是空值。

从 android 端发送数据。代码从 android 和 java 服务器端正确编译。 当我在网页上运行该程序时,我得到空值。

感谢任何帮助

最佳答案

您在服务器端代码中使用 doGet,但在客户端调用 HttpPost。更改其中任何一个。

服务器代码:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {

...
}

客户端代码:

HttpGet httppost = new HttpGet("http://primecalculation.appspot.com/");

但不是两者兼而有之

关于java - 从android应用程序接收java servlet中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31705970/

相关文章:

java - JRuby Java 版本兼容性

android - dexopt 和 dex2oat 的区别?

java - 缺少 android 权限时适合抛出什么异常?

java - 使用 Html 标签和 servlet 自动完成

java - HTML 页面作为 AJAX 响应?

java - 保存SQL数据库条目的创建日期

java - 为什么 Mule flow default-exception-strategy 没有效果?

android - 此 RelativeLayout 布局或其 LinearLayout 父级无用

Java JSP Jquery AJAX Servlet

java - 使方法最终化的推理