如何从Android的数据发送到谷歌在Java做应用程序引擎的Web应用程序

问题描述:

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

我是新来的Google App Engine和android.Made在Android的应用程序从用户的取值,并张贴到的servlet。
我用的HttpClient发布在Android与Servlet数据,本地托管的以及从仿真器发送数据。
我收到空值从servlet的side.I GOOGLE了很多在这个背景下,但没有得到解决的issue.Any帮助AP preciated.Thnx

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

下面是一个简单的例子:
http://webdeveloperpadawan.blogspot.co.uk/2015/01/google-app-engine-and-android-playing.html

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

请注意您定义的谷歌应用程序引擎的后端服务的方式,然后调用它的Andr​​oid code是这样的:

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

myApiService.sayHi(name).execute()

如果没有更多的细节我不知道你的错误是什么,但可以肯定你localY坐标托管的servlet首先运行。否则,你永远不会去。你应该能够在移动到Android code之前,一个基本的Web表单来测试这一点。

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.