从android应用程序接收java servlet中的空值

时间:2015-07-29 16:19:31

标签: java android servlets

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

我正在向servlet发送值,但是在servlet中我得到了null值。

从android端发送数据.Code是从android和java服务器端正确编译的。 当我在网页上运行程序时,我得到空值。

任何帮助赞赏thanx

1 个答案:

答案 0 :(得分:1)

您在服务器端代码中使用doGet,但在客户端调用HttpPost。改变其中一个。

服务器代码:

def DelFile(self, address, del_file):
    cmd_line = r'psexec \\{0} cmd /c "del /f /q "{1}" && if exist "{1}" (exit /b 1) else (exit /b 0)"'.format(address, del_file)

    myP = subprocess.Popen(cmd_line, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

    out = timeout(myP.communicate, (), {}, 60, (None, None))
    result = out[0]
    error = out[1]
    error_code = None
    error_code = myP.returncode
    notes = []

    return cmd_line, result, error, error_code, notes

客户代码:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {

...
}

但不能同时