开发者

Get the returning url after POST in android

开发者 https://www.devze.com 2023-03-17 19:19 出处:网络
I have to post some data to an url, here\'s my code. After this server must redirect me to a new URL, but hpw do i get it in and开发者_如何学Croid?

I have to post some data to an url, here's my code. After this server must redirect me to a new URL, but hpw do i get it in and开发者_如何学Croid?

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
    }
} 


You can get the url in php by using $_server and echo that string.

Instead of using

HttpResponse response = httpclient.execute(httppost);

use the following code

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

Now the response body string contains the url you echo from the php server

0

精彩评论

暂无评论...
验证码 换一张
取 消