Question

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

Was it helpful?

Solution

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();
            } 
        } 
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top