Android: How to send variable parameters with GetH

2019-08-29 02:47发布

问题:

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

回答1:

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.



回答2:

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


标签: android post get