1
我想使用Android客户端和Glassfish 4.1.1服务器(通过REST服务进行通信)实现基本身份验证。 该服务运行良好(由POSTMAN和另一个C#-Client验证),但在Android上,现在让我疯狂。 它也似乎在服务器端收到的对象是“null”,在Android端也会引发恼人的“EOFException”。 服务器端(正常工作)将对象发布到泽西岛时Android客户端(API 23)上的java.io.EOFException
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Account validate(Account acc)
{
Account a = null;
a = Database.getInstance().getAccountByUserPw(acc);
return a;
}
Android客户端:
public Account postData(String JSONtoSend)
{
URL url;
Account get = new Account();
try {
url = new URL("http://192.xxx.xxx.x:18080/HolidayOutServer/webresources/validateacc");
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
urlCon.setRequestMethod("POST");
urlCon.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlCon.setRequestProperty("Accept", "application/json; charset=UTF-8");
urlCon.setDoOutput(true); // to be able to write.
urlCon.setDoInput(true); // to be able to read.
OutputStreamWriter out = new OutputStreamWriter(urlCon.getOutputStream());
out.write(JSONtoSend);
ObjectInputStream ois = new ObjectInputStream(urlCon.getInputStream());
get = (Account) ois.readObject();
return get;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return get;
}
被称为内这样的:
class help extends AsyncTask<String, Void, Account>
{
@Override
protected Account doInBackground(String... params) {
return postData(new Gson().toJson(new Account("aleqs", "lexx", -2)));
}
}
问题一言以蔽之:
- 新泽西服务器收到null
- Android抛出这个荒谬的EOFException。
有人可以帮忙吗? 在此先感谢, 约翰。