2012-03-03 77 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