Question for Android developpement

35 views
Skip to first unread message

Jéremy Weber

unread,
Dec 9, 2013, 3:35:53 PM12/9/13
to jso...@googlegroups.com
Hi,
I use your library for my project which by the way is very useful thank you. I create a desktop application which when a person want to log in in the system send an JSONObject with his username and password. Then the server send back the response if the person is allowed or not. Until here everything works fine. 
Now i want to make an Android app. I take the same code than before for login but the application crashes when it arrives at JSONObject.readObject();
The code for login is launch from an AsyncTask and i put the permission to access to Internet to the Manifest. I would like to know if the JsonReader /Writer etc works fine on Android. 

I see that readObject can't find a proper way to read the JSON but i don't understand why. 
I put the log in annex.

Thank you.

P.S I'm not an expert at all on Android so maybe something missing in my code.
Hope that my english isn't too bad :)


log.txt

John DeRegnaucourt

unread,
Dec 9, 2013, 11:49:22 PM12/9/13
to jso...@googlegroups.com
This line right here:

 java.io.IOException: Input is invalid JSON; does not start with '{' or '[', c=-1

tells me that the JSON that is making it to the device is not valid JSON format.  A JSON String must start with { or [.  Do you have a dump of the actual JSON that was transmitted?  That would help solve this issue more quickly.

Best regards,
John



--
You received this message because you are subscribed to the Google Groups "json-io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-io+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jéremy Weber

unread,
Dec 10, 2013, 2:48:56 AM12/10/13
to jso...@googlegroups.com
Thank you for you quick response.
Yes i saw that line too. I print the JSON just before to send it to the server and the JSON was fine {"type":"LOGIN", "username":"bla": ...} but on the server side the json arrives empty. I tried to hard code the JSON on the server side to see what happens and once again the JSON arrive empty at the Android app.And this is strange knowing that the server works well with a desktop app. 

Here the code i wrote : 


protected Boolean doInBackground(Void... params) {
// 3 : create a JSON object and send it to server for verification
JSONObject loginJS = new JSONObject();

try {
loginJS.put("type", Type.LOGIN);   // Type is an enum 
loginJS.put("username", userName);
loginJS.put("hash", mPassword);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
User user = new User();
System.out
.println("User created and ready to connect to the spu for login");
user.connexionToSPU(); // This method works
System.out.println("LoginJS = "+loginJS.toString()); At this point the print of the JSON is fine we have {"type":"Login", ....}
user.sendToSPU(loginJS); //This is where there's a problem
// 4 : wait response
JSONObject loginResponseJS = user.receiveFromSPU(); // And when i hard code the JSON on the server side receiveFromSPU get a empty JSON
--------------------------------------------------
Method connexionToSPU:

public void connexionToSPU() {
jswSPU = null;
jsrSPU = null;
Socket socket = null;
try {
socket = new Socket(prop.readPropertiesXML("IP_adress_server"),
Integer.parseInt(prop.readPropertiesXML("port_server")));
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
jswSPU = new JsonWriter(socket.getOutputStream());
jsrSPU = new JsonReader(new BufferedInputStream(socket.getInputStream()));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
---------------------------------------------------

sendToSPU method

public void sendToSPU(JSONObject json) {

try {
jswSPU.write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
--------------------------------------------
ReceiveFromSPU method

public JSONObject receiveFromSPU() {
JSONObject json = null;
try {
json = (JSONObject) jsrSPU.readObject();
System.out.println("JSON FROM THE SERVER : "+json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
---------------------------


Hope it's sufficiently clear. Thanks again for the help.

Jéremy Weber

unread,
Dec 10, 2013, 11:30:01 AM12/10/13
to jso...@googlegroups.com
Hi,
I got some news.
We tried to convert the JSON into byte[] and then on the other side we re convert it to string and this time it worked. But we don't want to convert all our JSON to byte[]. So it's the android that doesn't like the json and change it.
Maybe we have to add an other PERMISSION ?
Thank you

Jéremy Weber

unread,
Dec 10, 2013, 11:58:43 AM12/10/13
to jso...@googlegroups.com
Hi,
One more time we just tried to send just a String and it worked as well. But not the JSON
Reply all
Reply to author
Forward
0 new messages