开发者

Parsing the response from post method

开发者 https://www.devze.com 2023-02-21 05:39 出处:网络
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);

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


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();
            } 
        } 
    } 
0

精彩评论

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