nader nader - 5 months ago 13
JSON Question

Reading REST response from NSstring on Java Backend

I am getting the following error when I try to read the response from a get at my server:

JSON
Error: org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]


The client is an IOS app written in Objective C. I am sending an NSString with the following encoding:

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];


This is the nslog output of that NSString:

{
"longitude" : "-122.03121860000000253876",
"latitude" : "37.33233141000000188114"
}


Here is my get function at the java server:

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String get(String result) throws JSONException, IOException {
JSONObject sit = null;
try {
System.out.println(result + "JSON");
sit = new JSONObject(result);
System.out.println("Success: json = ");
System.out.println(sit.toString(2));
} catch(JSONException ex) {
System.out.println("Error: " + ex);
}
return this.dBQ.getSItems(sit).toString();
}


I am wondering if you guys can point me in the right direction.Ironically enough I am able to get the response from the server from the client. But for some reason the variable result in my java function ( at the server) outputs as empty and spits out an exception.

Thanks

Answer

Actually I answered my own question I used a post request with the following code:

  private DBQuery dBQ = new DBQuery();
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public String getresponses(String si) {
      return this.dBQ.getItems(si).toString();
  } 
Comments