1

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

1 回答 1

1

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

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

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

然后,您可以将构造的查询字符串附加到 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 编码(但不是&)。

HTH。

于 2013-09-27T11:31:17.437 回答