Java JSON io编写器在Android上删除JSONObject
我目前正在使用库JSON io。我创建了一个桌面应用程序,当一个人想要登录系统时,它会发送一个带有用户名和密码的JSONObject。然后,如果这个人被允许或不是JSONObject,服务器会发回响应。在这之前一切都很好。 现在我想做一个Android应用程序。我使用与以前相同的代码登录,但应用程序崩溃。 登录代码是从AsyncTask启动的,我将访问Internet的权限放在清单中 我执行了一些测试,发现JSONWriter的write方法删除了我的JSON,因为在服务器端他接收到:{}。我尝试在服务器端硬编码JSONObject服务器的代码运行良好,因为我们可以使用桌面应用程序,但这次android应用程序上的readObject也接收{}。 我们尝试发送jsonObject.toString,这次成功了,只是服务器没有配置为处理字符串 有人知道为什么android上的write和readObject两种方法都在删除JSON吗 多谢各位 编辑: 下面是我写的代码: 在android应用程序上Java JSON io编写器在Android上删除JSONObject,java,android,json,Java,Android,Json,我目前正在使用库JSON io。我创建了一个桌面应用程序,当一个人想要登录系统时,它会发送一个带有用户名和密码的JSONObject。然后,如果这个人被允许或不是JSONObject,服务器会发回响应。在这之前一切都很好。 现在我想做一个Android应用程序。我使用与以前相同的代码登录,但应用程序崩溃。 登录代码是从AsyncTask启动的,我将访问Internet的权限放在清单中 我执行了一些测试,发现JSONWriter的write方法删除了我的JSON,因为在服务器端他接收到:{}。我尝
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
方法CONNEXIONTSPU:
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();
}
}
森托斯普法
public void sendToSPU(JSONObject json) {
try {
jswSPU.write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
从SPU方法接收
public void sendToSPU(JSONObject json) {
try {
jswSPU.write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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;
}
希望它足够清晰。
谢谢这是一个项目,我们有点紧张,所以我们决定更改服务器并发送字符串,然后将服务器上的对象转换为JSONObject并重新发送到字符串。但如果有人知道它为什么不起作用,我还是会开的。