dongzhizhai4070 2013-09-27 11:12 采纳率: 0%
浏览 30

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

I am trying to send via my Android app this message:

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

where param1, param2 and param3 are values the user inputs in EditText fields...

My app for the moment reads this values, but when I try to send them with httppost, it's not working (I have no error message, but the values are not changing as they are supposed to do...)

Can someone explain what I'm doing wrong?

Here is a part of my MainActivity where I read the values of the parameters and call the sendPostRequest method:

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


      }; 

and here is the http post method, defined later in my 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条回答 默认 最新

  • dpdhf02040 2013-09-27 11:31
    关注

    This form https://myserver/index.php?x0=param1&y0=param2&z0=param3 is not the one that you will get if you make a POST-Request. You should either try a GET to send it in this form or closely look at the server side of your project if POST vs. GET makes a difference there.

    (edited to add information for the comment of user2748484)

    The values that you assign to a variable is not static by definition (hence "variable"). If you want to send key-value-pairs in your GET-Request the most basic approach is to construct the Query-String (the part after the ?) yourself, for instance by using a StringBuilder in Java.

    You may then just append the constructed query-String to the URI.

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

    You may then use the result String to execute your GET-Request. Nota bene: if you construct your query-String don't forget to URL-Encode the keys and the values (but not the &).

    HTH.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP