user5101829
user5101829

Reputation:

How to send data from android to google app engine web app made in java

private String s;
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);
    //s=numDisplay.toString();  
    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){
            //pb.setVisibility(View.GONE);
            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://1-dot-primecalculation.appspot.com/");

            try {

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("id0", 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
            }
        }



   }
 }

**Server Side Code**
private static final long serialVersionUID = 1L;
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    processRequest(req, resp);

    String user = req.getParameter("id0");
    //resp.setContentType("text/plain");
    PrintWriter writer = resp.getWriter();
     writer.write("value" + user);
}
private void processRequest(HttpServletRequest req, HttpServletResponse resp)throws IOException {
// TODO Auto-generated method stub
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    doPost(req, resp);
}}

I am new to google app engine and android.Made an application in android which takes value from user and post it to servlet. I used httpClient to post data from android to servlet,hosted locally and sending data from emulator . I receive null value from servlet side.I googled a lot in this context but not getting solution for the issue.Any help appreciated.Thnx

Upvotes: 1

Views: 44

Answers (1)

James
James

Reputation: 3555

Here's a simple example: http://webdeveloperpadawan.blogspot.co.uk/2015/01/google-app-engine-and-android-playing.html

Note the way you define the Google app engine backend service, and then call it in Android code like this:

myApiService.sayHi(name).execute()

Without more specifics I'm not sure what your error is, but be sure your localy hosted servlet is running first. Otherwise you'll never get going. You should be able to test this with a basic web form before moving on to the Android code.

Upvotes: 1

Related Questions