解析 post 方法的响应

发布于 2024-10-29 19:41:04 字数 1179 浏览 3 评论 0原文

我正在将数据发布到服务器,并以响应的形式返回发布数据...... 我使用这段代码

PostMethod post = new PostMethod(this._serverUrl);

        InputStream is=null;
        is = new ByteArrayInputStream(postData.getBytes("UTF-8"));

        post.setRequestEntity(new InputStreamRequestEntity(is));
        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

        HttpClient httpclient = new HttpClient();

        int result = httpclient.executeMethod(post);

        String response="";
        response    =   post.getResponseBodyAsString();

我已经使用 commons apache httpclient 来发帖...... 在这里我得到的响应是

03-31 17:53:49.192: INFO/Response(2237): <?xml version="1.0" encoding="UTF-8"?>
03-31 17:53:49.192: INFO/Response(2237): <AndroidGame>
03-31 17:53:49.192: INFO/Response(2237):   <Result>0</Result>
03-31 17:53:49.192: INFO/Response(2237):   <ErrorCode>509</ErrorCode>
03-31 17:53:49.192: INFO/Response(2237):   <ErrorMsg>You are using wrong Super Password</ErrorMsg>
03-31 17:53:49.192: INFO/Response(2237): </AndroidGame>

但我需要得到字符串中的响应...我无法在单个字符串中获得响应...它是成块的...任何人都可以帮助我吗

i am posting the data to a server and i get the post data back in form of response...
I use this code

PostMethod post = new PostMethod(this._serverUrl);

        InputStream is=null;
        is = new ByteArrayInputStream(postData.getBytes("UTF-8"));

        post.setRequestEntity(new InputStreamRequestEntity(is));
        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

        HttpClient httpclient = new HttpClient();

        int result = httpclient.executeMethod(post);

        String response="";
        response    =   post.getResponseBodyAsString();

I have used commons apache httpclient to do posting....
here i get response as

03-31 17:53:49.192: INFO/Response(2237): <?xml version="1.0" encoding="UTF-8"?>
03-31 17:53:49.192: INFO/Response(2237): <AndroidGame>
03-31 17:53:49.192: INFO/Response(2237):   <Result>0</Result>
03-31 17:53:49.192: INFO/Response(2237):   <ErrorCode>509</ErrorCode>
03-31 17:53:49.192: INFO/Response(2237):   <ErrorMsg>You are using wrong Super Password</ErrorMsg>
03-31 17:53:49.192: INFO/Response(2237): </AndroidGame>

But i need to get the response in String.... I am not able to get the response in a single string... it comes in chunks... Can anyone help me in this

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

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

发布评论

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

评论(1

深海少女心 2024-11-05 19:41:04

我这样做(我已经统一了我使用的不同方法的代码,所以它可能有点混乱):

    HttpPost request = new HttpPost(url);
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair(PARAMETER_LOGIN, login));
    postParameters.add(new BasicNameValuePair(PARAMETER_PASSWORD, password));
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, "UTF-8");
    request.setEntity(formEntity);

    HttpResponse response = client.execute(request);

    BufferedReader in = null; 
    try {
        //Log.d("status line ", "test " + response.getStatusLine().toString());
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 
        String NL = System.getProperty("line.separator"); 
        while ((line = in.readLine()) != null) { 
            sb.append(line + NL); 
        } 
        in.close(); 
        return sb.toString();       
    } finally { 
        if (in != null) { 
            try { 
                in.close(); 
            } catch (IOException e) { 
                e.printStackTrace();
            } 
        } 
    } 

I do it this way (Ive united code from different methods I use so it could be a bit messy):

    HttpPost request = new HttpPost(url);
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair(PARAMETER_LOGIN, login));
    postParameters.add(new BasicNameValuePair(PARAMETER_PASSWORD, password));
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, "UTF-8");
    request.setEntity(formEntity);

    HttpResponse response = client.execute(request);

    BufferedReader in = null; 
    try {
        //Log.d("status line ", "test " + response.getStatusLine().toString());
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 
        String NL = System.getProperty("line.separator"); 
        while ((line = in.readLine()) != null) { 
            sb.append(line + NL); 
        } 
        in.close(); 
        return sb.toString();       
    } finally { 
        if (in != null) { 
            try { 
                in.close(); 
            } catch (IOException e) { 
                e.printStackTrace();
            } 
        } 
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文