开发者

Get the returning url after POST in android

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜