简体   繁体   English

从Android应用程序接收Java Servlet中的空值

[英]Receiving null value in java servlet from android application

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

I am sending value to servlet but in servlet I am getting null value . 我正在向servlet发送值,但是在servlet中,我得到了空值。

From android side data is sent .Code is compiled correctly from android and java server side. 从android端发送数据。从android和java服务器端正确编译代码。 When I run the program on webpage I get null value . 当我在网页上运行程序时,我得到空值。

Any help appreciated thanx 任何帮助感谢thanx

You're using doGet in server side code but calling HttpPost in client side. 您在服务器端代码中使用doGet,但在客户端调用HttpPost。 Change either one of them. 更改其中之一。

Server Code : 服务器代码:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {

...
}

OR 要么

Client Code: 客户代码:

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

BUT NOT BOTH 但不是两者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM