user5101829
user5101829

Reputation:

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 .

From android side data is sent .Code is compiled correctly from android and java server side. When I run the program on webpage I get null value .

Any help appreciated thanx

Upvotes: 1

Views: 114

Answers (1)

Harish Sridharan
Harish Sridharan

Reputation: 1090

You're using doGet in server side code but calling HttpPost in client side. 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

Upvotes: 1

Related Questions