android中获取POST后返回的url

发布于 2024-11-18 10:30:45 字数 801 浏览 3 评论 0原文

我必须将一些数据发布到网址,这是我的代码。此服务器必须将我重定向到新的 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
    }
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-11-25 10:30:45

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

而不是使用

HttpResponse 响应 =
httpclient.execute(httppost);

使用以下代码

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文