2011-03-31 102 views
0

我張貼的數據到服務器,我得到的數據後迴響應的形式...... 我使用此代碼解析來自POST方法

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

我已經使用公共阿帕奇的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> 

但我需要得到字符串響應....我不能夠得到一個字符串的反應......它有大塊...任何人都可以幫我在這

回答

1

我做這種方式(IVE團結從我用這樣不同的方法的代碼也可能是有點亂):

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

嘿馬克西姆...:d ...ü在這HttpResponse對象響應使用什麼樣的客戶端= client.execute(request);你可以指定嗎?...... – 2011-03-31 13:52:26

+0

HttpClient client = new DefaultHttpClient(); – Maxim 2011-03-31 13:58:38