1

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
4
  • On server side you are not getting changed values or what? Commented Sep 27, 2013 at 12:19
  • 1
    yes, no error msg but on server change I don't have the values I expect... Commented Sep 27, 2013 at 12:33
  • in doinbackground print the values of x00, y00, z00 and see those are updated values or not Commented Sep 27, 2013 at 12:38
  • I'm not sur I understand what you mean...I know the values x00, y00 and z00 (user input in the app) and I want to send them to a xml file on the server - so I see if it works or not just by having a look at the xml file on the server - if the values are the ones I send it's OK, if not, it did not worked. I tried another method (stackoverflow.com/questions/19057618/…), but I managed to send only fixed values, not the ones I receive from user input... Commented Sep 27, 2013 at 20:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

but if just I send it this way with GET, I can not send variables, no? I mean I can just send some fixed values of param1, 2, and 3...and I want to send something that changes on user input, how is this working?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.