0

I'm new in Android and I am blocked with a problem I don't know how to solve: from my app I'm trying to send a message of this form to a server in order to modify a xml file on the server:

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

I managed to make it work with fixed values of param1, param2 and param3, i.e. using the code below I modify the values of the xml file on the server to 1, 2 and 3:

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

        new RequestTask().execute(https://myserver/index.php?x0=1&y0=2&z0=3);
        }           
      };

class RequestTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (IOException e) {
                    //TODO Handle problems..
                }
                return responseString;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //Do anything with response..
            }
        }   

But my problem is that I want to send not fixed values, but the values (variables) which are input by the user and are read in the "InitialPosListener" : x00, y00 and z00...

Is there a way to do this? Thanks a lot

2 Answers 2

0

It looks like you are just executing HTTP GET to your server. Calling

new RequestTask().execute("https://myserver/index.php?x0=" + x00 + "&y0=" + y00 + "&z0=" + z00);

will do what you want.

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

2 Comments

I just realized have a new problem here: it works, but only one: when I click the first time on the button, the app send to the server the values of x00, y00 and z00 - but if I click the button once again, with new values of x00, y00 and z00, the new values are not send to the server any more...By any chance you know why this happens? Thanks
I would need to look at the code. Do you have any class members used?
0

In doInBackground(String... uri) method, try something like this:

DefaultHttpClient client1 = new DefaultHttpClient();
HttpResponse response = null;
HttpGet httpGet = null;

try {
    httpGet = new HttpGet(URL); 
    response = client1.execute(httpGet);
    ......
}

Comments

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.