Android 网络请求

发布于 2024-12-14 12:49:54 字数 985 浏览 3 评论 0原文

我正在尝试将 JSON 字符串发送到服务器并从 Web 服务器检索结果 JSON。我能够处理发布部分,但不确定如何在同一请求中检索结果。

private boolean sendFacebookDataToServer(String url)
{
    // conect to server
    // This method for HttpConnection
    boolean isDataSend=false;
    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(url);

        List<NameValuePair> value = new ArrayList<NameValuePair>();

        value.add(new BasicNameValuePair("facebook", createJsonFormatDataString()));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);

        request.setEntity(entity);

        HttpResponse res = client.execute(request);

        String[] status_String=res.getStatusLine().toString().trim().split(" ");

        if(status_String[1].equals("200")){
            isDataSend= true;
        }


    } catch (Exception e) {
        System.out.println("Exp=" + e);
    }
    return isDataSend;
}

或者有什么文章我可以参考了解它是如何完成的?

I am trying to send a JSON string to the server and retrieve back a resulting JSON from the web server. I am able to handle the posting part but not sure how to retrieve back the result in the same request.

private boolean sendFacebookDataToServer(String url)
{
    // conect to server
    // This method for HttpConnection
    boolean isDataSend=false;
    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(url);

        List<NameValuePair> value = new ArrayList<NameValuePair>();

        value.add(new BasicNameValuePair("facebook", createJsonFormatDataString()));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);

        request.setEntity(entity);

        HttpResponse res = client.execute(request);

        String[] status_String=res.getStatusLine().toString().trim().split(" ");

        if(status_String[1].equals("200")){
            isDataSend= true;
        }


    } catch (Exception e) {
        System.out.println("Exp=" + e);
    }
    return isDataSend;
}

Or is there any article i can refer to understand how its done ?

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

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

发布评论

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

评论(1

木森分化 2024-12-21 12:49:54
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    String bufstring = EntityUtils.toString(res.getEntity(), "UTF-8");
    isDataSend= true;
}

响应数据将在 bufstring 中

编辑:

只需将您的代码(如下)替换为我的

    String[] status_String=res.getStatusLine().toString().trim().split(" ");

    if(status_String[1].equals("200")){
        isDataSend= true;
    }
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    String bufstring = EntityUtils.toString(res.getEntity(), "UTF-8");
    isDataSend= true;
}

response data will be in bufstring

EDIT:

just replace your code(bellow) with my

    String[] status_String=res.getStatusLine().toString().trim().split(" ");

    if(status_String[1].equals("200")){
        isDataSend= true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文