Android:如何使用GetHttp发送变量参数?

时间:2013-09-27 18:25:10

标签: android post get

我是Android的新手,我遇到了一个我不知道如何解决的问题: 从我的应用程序我正在尝试将此表单的消息发送到服务器,以便修改服务器上的xml文件:

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

我设法使用param1,param2和param3的固定值,即使用下面的代码我将服务器上的xml文件的值修改为1,2和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..
            }
        }   

但我的问题是我想发送不是固定值,而是发送由用户输入并在“InitialPosListener”中读取的值(变量):x00,y00和z00 ...

有办法做到这一点吗?非常感谢

2 个答案:

答案 0 :(得分:0)

看起来您只是在服务器上执行HTTP GET。调用

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

会做你想做的事。

答案 1 :(得分:0)

在doInBackground(String ... uri)方法中,尝试这样的事情:

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

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