如何将数据从Android发送到java制作的谷歌应用引擎网络应用程序

时间:2015-08-04 11:30:16

标签: android google-app-engine java-ee-7

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

我是谷歌应用程序引擎和android.Made的新应用程序在android中从用户获取值并将其发布到servlet。 我使用httpClient将数据从android发布到servlet,在本地托管并从模拟器发送数据。 我从servlet端收到null值。我在这个上下文中搜索了很多但没有得到问题的解决方案。任何帮助赞赏.Thnx

1 个答案:

答案 0 :(得分:1)

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

请注意您定义Google应用引擎后端服务的方式,然后在Android代码中调用它:

myApiService.sayHi(name).execute()

如果没有更多细节,我不确定您的错误是什么,但请确保您的localy托管的servlet首先运行。否则你永远不会开始。在继续使用Android代码之前,您应该能够使用基本的Web表单进行测试。