Android:如何用http发送用户输入值?

时间:2013-09-27 11:12:36

标签: php android http-post

我正试图通过我的Android应用发送此消息:

https://myserver/index.php?x0=param1&y0=param2&z0=param3

其中param1,param2和param3是用户在EditText字段中输入的值...

我的应用程序暂时读取此值,但是当我尝试使用httppost发送它时,它不起作用(我没有错误消息,但值不会像它们应该做的那样改变...)

有人可以解释我做错了吗?

以下是我的MainActivity的一部分,我在其中读取参数的值并调用sendPostRequest方法:

 private OnClickListener InitialPosListener = new OnClickListener() {
        @Override
        public void onClick(View v) {

        String x00 = InitialPosX.getText().toString(); String y00 = InitialPosY.getText().toString(); String z00 = InitialPosZ.getText().toString();
        float x0 = Float.valueOf(x00); float y0 = Float.valueOf(y00); float z0 = Float.valueOf(z00);


        sendPostRequest(x00, y00, z00);


      }; 

这是http post方法,稍后在我的MainActivity中定义:

        private void sendPostRequest(String x00, String y00, String z00) {

            class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{

                @Override
                protected String doInBackground(String... params) {

                    String x00 = params[0];
                    String y00 = params[1];
                    String z00 = params[2];

                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("https://myserver/index.php");
                    BasicNameValuePair XBasicNameValuePair = new BasicNameValuePair("x0", x00);
                    BasicNameValuePair YBasicNameValuePAir = new BasicNameValuePair("y0", y00);
                    BasicNameValuePair ZBasicNameValuePAir = new BasicNameValuePair("z0", z00);

                    List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
                    nameValuePairList.add(XBasicNameValuePair);
                    nameValuePairList.add(YBasicNameValuePAir);
                    nameValuePairList.add(ZBasicNameValuePAir);


                    try {
                        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
                        httpPost.setEntity(urlEncodedFormEntity);

                        try {
                            HttpResponse httpResponse = httpClient.execute(httpPost);
                            InputStream inputStream = httpResponse.getEntity().getContent();
                            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                            StringBuilder stringBuilder = new StringBuilder();
                            String bufferedStrChunk = null;

                            while((bufferedStrChunk = bufferedReader.readLine()) != null){
                                stringBuilder.append(bufferedStrChunk);
                            }

                            return stringBuilder.toString();

                        } catch (ClientProtocolException cpe) {
                            System.out.println("First Exception caz of HttpResponese :" + cpe);
                            cpe.printStackTrace();
                        } catch (IOException ioe) {
                            System.out.println("Second Exception caz of HttpResponse :" + ioe);
                            ioe.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException uee) {
                        System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
                        uee.printStackTrace();
                    }

                    return null;
                }   


            } //END CLASS SendPostReqAsyncTask  

            SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
            sendPostReqAsyncTask.execute(x00, y00, z00); 

        } //END VOID sendPostRequest

1 个答案:

答案 0 :(得分:1)

如果您提出https://myserver/index.php?x0=param1&y0=param2&z0=param3 - 请求,则此表单POST不是您将获得的表单。您应该尝试GET以此形式发送,或者如果POSTGET在那里产生差异,请仔细查看项目的服务器端。

(已编辑以添加user2748484评论的信息)

根据定义,您赋给变量的值不是静态的(因此是“变量”)。如果要在GET中发送键值对 - 请求,最基本的方法是自己构造Query-String(?之后的部分),例如使用{{ Java中的1}}。

然后,您可以将构造的query-String附加到URI。

StringBuilder

然后,您可以使用结果字符串来执行String url = "https://myserver/index.php"; String queryString = "x0=param1&y0=param2&z0=param3"; // construct this with StringBuilder String result = url + "?" + queryString; - 请求。 Nota bene:如果你构造你的查询 - 字符串,不要忘记对键和值进行URL编码(而不是GET)。

HTH。