2012-03-03 79 views
0

我有一個java web服務,如果登錄成功,必須返回響應無法打印JSON-RPC響應

服務器端方法的返回線

return new Response(new InfoSessionJson(newKey, is), null, id); 

獲得響應我曾嘗試使用代碼

HttpResponse response = mClient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 
if(response != null){ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       loggato=true; 
       try { 
        while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       System.out.println("The response is "+sb.toString()); 

但輸出打印返回Apache的錯誤

我試圖打印的

HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 

直接輸出與

System.out.print(entity); 
    System.out.print(response); 

這種打印:

[email protected] org.apache .http.message.BasicHttpResponse @ 40574a00

錯誤在哪裏?

我無法正確解析響應或問題出現在?

WebService的方法有這個FIM

@Webservice(paramNames = {"email", "password", "stayLogged", "idClient"}, 
public Response startSession(String email, String password, Boolean stayLogged, String idClient) 

我送一個JSON與

StringEntity stringEntity = new StringEntity(jsonObject.toString()); 
        httpost.setEntity(stringEntity); 
        httpost.setHeader("Accept", "application/json"); 
        httpost.setHeader("Content-type", "application/json"); 

如果發送的JSON對象是

{"method":"startSession","params":[{"email":"[email protected]","password":"1234","idClient":"ANDROID","stayLogged":"1"}]} 

的web服務工作正常使用iOS應用程序,但不會與我的Android應用程序,

我在此線程中描述的過程中的錯誤在哪裏?

我希望有人能幫助我。

回答

1

我應該使用的代碼

stringEntity.setContentEncoding(HTTP.UTF_8); 

設置編碼方式爲UTF, 與此相關,服務器接受JSON請求,並給予正確的迴應

0

嘗試:System.out.println("The response is "+sb.toString());

line在每個循環被擦除。

+0

謝謝你,我已經糾正這一錯誤並打印一些東西......但錯誤,而不是正確的響應與會話密鑰,我不明白爲什麼 – AndreaF 2012-03-03 18:00:00

0

我幾乎完全相同的代碼,它對我來說工作正常,也許有一個問題與您發送的請求。看看響應狀態代碼以檢查錯誤:

status = response.getStatusLine(); 
status_code = status.getStatusCode(); 

有可能是在服務器端的一些錯誤導致你得到一個空的500響應或東西。

+0

我得到許多行 sun.reflect.GeneratedMethodAccessor1570.invoke(未知源) 03-03 18: 04:14.869:INFO/System.out(1360):sun.reflect.DelegatingMethodAccessorImpl.in voke(DelegatingMethodAccessorImpl.java:43) 等 與iOS的web服務工作正常 – AndreaF 2012-03-03 18:06:39

+0

狀態代碼爲500 – AndreaF 2012-03-03 18:13:32

+0

好吧,我不知道你是否熟悉[HTTP響應代碼(HTTP:/ /www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)。 500狀態碼意味着管理您的請求的服務器發生錯誤。我建議嘗試複製一個簡單的客戶端發送的完全相同的請求(比如'curl'),或者更好的是自動測試,然後調試服務器。 爲此,首先確定您要發送給服務器的什麼(標題和內容),也許您可​​以告訴我們您是如何創建請求的,我們可以爲您提供幫助。 – 2012-03-03 18:20:19