在android中POST后获取返回的url

时间:2011-07-06 05:35:21

标签: java android apache httpclient

我必须将一些数据发布到网址,这是我的代码。在此服务器必须将我重定向到一个新的URL,但是hpw我是否在android中获取它?

public void postData() {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "data"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

1 个答案:

答案 0 :(得分:3)

你可以使用$ _server来获取php中的url并回显该字符串。

而不是使用

  

HttpResponse response =   httpclient.execute(httppost);

使用以下代码

 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
  String ResponseBody = httpClient.execute(httppost, responseHandler);

现在响应正文字符串包含您从php服务器回显的URL