【问题标题】:Android: how to send user input values with http post?Android:如何使用 http post 发送用户输入值?
【发布时间】:2013-09-27 11:12:36
【问题描述】:

我正在尝试通过我的 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

【问题讨论】:

  • 在服务器端你没有得到改变的值还是什么?
  • 是的,没有错误消息,但是在服务器更改时我没有我期望的值...
  • 在 doinbackground 中打印 x00、y00、z00 的值,看看这些值是否更新
  • 我不知道我明白你的意思...我知道 x00、y00 和 z00 的值(用户在应用程序中输入),我想将它们发送到服务器上的 xml 文件- 所以我通过查看服务器上的 xml 文件来查看它是否有效 - 如果值是我发送的值,那没关系,如果没有,它就不起作用。我尝试了另一种方法(stackoverflow.com/questions/19057618/…),但我设法只发送固定值,而不是我从用户输入收到的值...

标签: php android http-post


【解决方案1】:

如果您提出POST-Request,则不会获得此表单https://myserver/index.php?x0=param1&amp;y0=param2&amp;z0=param3。如果POSTGET 在那里有所不同,您应该尝试GET 以这种形式发送它或仔细查看项目的服务器端。

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

根据定义,您分配给变量的值不是静态的(因此是“变量”)。如果您想在GET-Request 中发送键值对,最基本的方法是自己构造查询字符串(? 之后的部分),例如在 Java 中使用StringBuilder

然后您可以将构造的查询字符串附加到 URI。

String url = "https://myserver/index.php";
String queryString = "x0=param1&y0=param2&z0=param3"; // construct this with StringBuilder
String result = url + "?" + queryString;

然后您可以使用结果字符串来执行您的GET-Request。注意:如果您构建查询字符串,请不要忘记对键和值进行 URL 编码(但不是 &amp;)。

HTH。

【讨论】:

  • 但如果我只是用 GET 以这种方式发送它,我不能发送变量,不是吗?我的意思是我可以只发送 param1、2 和 3 的一些固定值......我想发送一些在用户输入时发生变化的东西,这是如何工作的?
猜你喜欢
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多