0

I have a java webservice, that must return a response if the login is successful

The return line of the server side method is

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

To get the response I have tried to use the code

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

But the output print returns an apache error

I have tried to print the output of the

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

directly with

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

and this print:

org.apache.http.conn.BasicManagedEntity@40575ac8 org.apache.http.message.BasicHttpResponse@40574a00

Where is the error??

I cannot parse the response correctly or the problem is before???

the webservice method has this fim

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

and I send a json with

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

Where the json object sent is

{"method":"startSession","params":[{"email":"test.web@yahoo.it","password":"1234","idClient":"ANDROID","stayLogged":"1"}]}

The webservice works fine with an iOS app but wont work with mine android app,

Where is the error in the procedure that I have described in this thread???

I hope someone can help me.

AndreaF
  • 11,975
  • 27
  • 102
  • 168

3 Answers3

1

I should use the code

stringEntity.setContentEncoding(HTTP.UTF_8);

to set the encoding mode to UTF, with this, the server accept the json request and give the correct response

AndreaF
  • 11,975
  • 27
  • 102
  • 168
0

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

line is erased at each loop.

Francois
  • 10,730
  • 7
  • 47
  • 80
  • Thank you, I have correct this error and print something... but an error and not the right response with the session key, and I don't understand why – AndreaF Mar 03 '12 at 18:00
0

I have almost the exact same code and it works fine for me, maybe there is a issue with the request that you are sending. Look at the response status code to check for errors:

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

There may be some error on the server side causing you to get an empty 500 response or something.

  • i get many lines with sun.reflect.GeneratedMethodAccessor1570.invoke(Unknown Source) 03-03 18:04:14.869: INFO/System.out(1360): sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) etc with iOS the webservice works fine – AndreaF Mar 03 '12 at 18:06
  • Ok, I don't know if you are familiar with the [Http Response Codes](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). A 500 status code means there was an error on the server managing your request. I would recommend trying to replicate the exact same request you are sending on a simpler client (like `curl`), or better, an automated test, and debugging the server. For that, first find out exactly what are you sending to the server (headers and content), maybe you could show us how are you creating your request and we could help you with that. – Juan Enrique Muñoz Zolotoochin Mar 03 '12 at 18:20
  • I have understand this Juan, but the server works fine with an iOS app... because of that, I don't understand Why with my code doesn't works. The server has a baselink of the type, http://ws.nameserver.com/ws.fga – AndreaF Mar 03 '12 at 18:26
  • I have used a DefaultHttpClient mClient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http:// ws.nameserver.com/ws.fga"); the others parts of code are in the first post – AndreaF Mar 03 '12 at 18:30
  • Mmhh... your code looks ok to me. Maybe the JSONObject is malformed? not conforming with what server expects? Anyway, since the error is on the server side (although it's clearly caused by a bad request if its working on the iOS client), if you have any control over the server code, try debugging from there, check exactly what the server is receiving, and compare it to what it receives from your working iOS client. – Juan Enrique Muñoz Zolotoochin Mar 03 '12 at 18:32
  • On iOs the request come from: NSLog(@"Request:\n%@", requestString); NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; [requestString release]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:BASEPATH]] autorelease]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:requestData]; where BASEPATH is http://ws.nameserver.com/ws.fga and requestString a string with the json – AndreaF Mar 03 '12 at 18:40
  • Can you share the exact JSON String being sent by each client? – Juan Enrique Muñoz Zolotoochin Mar 03 '12 at 18:54
  • the exactly json is {"method":"startSession", "params":["", "email@email.com", "password", "1", "IPHONE"], "id":1} I have changed the code to send this but same result... error 500 – AndreaF Mar 05 '12 at 02:59