我被困在服务器(java servlet)正在发送数据的时候,但是改造总是调用onFailure方法。在其他stackoverflow问题上,我发现这是服务器端错误,其中服务器未发送正确的json数据,但我无法识别如何解决此问题。
我的Java servlet代码在这里
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
System.out.println("Request received");
try (PrintWriter out = response.getWriter()) {
String username = request.getParameter(RequestParams.QUERY_USERNAME);
String password = request.getParameter(RequestParams.QUERY_PASSWORD);
AccountSignInModel model = new AccountSignInModel(username, password);
AccountBLL bll = new AccountBLL();
ResultSignInModel result = bll.signIn(model);
bll.close();
DetailBLL detailBll = new DetailBLL();
JSONObject obj = new JSONObject();
if (result != null) {
switch (result.getType()) {
// driver
case 1:
// get driver related record and send back to client
DriverBLL driverBll = new DriverBLL();
int driverId = driverBll.getDriverId(result.getAccountId());
driverBll.close();
JSONObject obj1 = detailBll.getDetailOFVehicleAndDriverForOwnerByDriverId(driverId);
obj = new JsonModelConverter(obj1).getSignInModel(true);
break;
// agency
case 0:
// get agency related record and send back to client
AgencyBLL agencyBll = new AgencyBLL();
int agencyId = agencyBll.getAgencyId(result.getAccountId());
agencyBll.close();
JSONObject obj2 = detailBll.getDetailOfAgencyForOwner(agencyId);
obj = new JsonModelConverter(obj2).getSignInModel(false);
break;
default:
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
throw new NoSuchObjectException("No Data Found");
}
detailBll.close();
out.println(obj);
}
else {
// invailid username or password
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(SignIn.class.getName()).log(Level.SEVERE, null, ex);
}
}
服务器生成的响应是这个(对于用户类型1)
{
"driverVehicleModel": {
"address": {
"hometown": "Lahore",
"phone": "3648434548",
"city": "Lahore",
"localAddress": "Lahore"
},
"name": "Corolla",
"account": {
"username": "j"
},
"vehicle": {
"vehicleColor": "White",
"vehicleName": "Corolla",
"vehiclePower": "1600 cc",
"vehicleModel": "2018",
"vehicleNumber": "khs 765",
"id": 102,
"vehicleType": "Car"
}
},
"agencyModel": ""
}
对于用户类型0,它正在响应此响应
{
"driverVehicleModel":"",
"agencyModel": {
"numOfVehicles":65,
"address": {
"hometown":"Gujranwala",
"phone":"3465544",
"city":"Gujranwala",
"localAddress":"Gujranwala"
},
"id":41,
"managerName":"shami",
"account": {
"username":"ssss"
},
"agencyName":"shami"
}
}
我的客户端(Android)代码在这里
登录界面方法
@POST(RequestParams.PATH_LOGIN)
Call<SignInModel> signIn(
@Query(RequestParams.QUERY_USERNAME) String username,
@Query(RequestParams.QUERY_PASSWORD) String password
);
SignInModel
public class SignInModel implements Serializable {
private DriverVehicleModel driverVehicleModel;
private AgencyModel agencyModel;
public SignInModel() {
}
public SignInModel(DriverVehicleModel driverVehicleModel, AgencyModel agencyModel) {
this.driverVehicleModel = driverVehicleModel;
this.agencyModel = agencyModel;
}
public SignInModel(AgencyModel agencyModel) {
this.agencyModel = agencyModel;
}
public SignInModel(DriverVehicleModel driverVehicleModel) {
this.driverVehicleModel = driverVehicleModel;
}
public DriverVehicleModel getDriverVehicleModel() {
return driverVehicleModel;
}
public void setDriverVehicleModel(DriverVehicleModel driverVehicleModel) {
this.driverVehicleModel = driverVehicleModel;
}
public AgencyModel getAgencyModel() {
return agencyModel;
}
public void setAgencyModel(AgencyModel agencyModel) {
this.agencyModel = agencyModel;
}
}
我对API的呼唤在这里
@OnClick(R.id.btn_sign)
public void btnSigClick(View view) {
attemptLogin();
}
private void attemptLogin() {
// Store values at the time of the login attempt.
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();
if (email.equals("") || password.equals("")) {
Toast.makeText(this, "Enter valid information", Toast.LENGTH_SHORT).show();
} else {
try {
password = Security.encryptPassword(password);
} catch (NoSuchAlgorithmException e) {
Toast.makeText(this, "Encryption error", Toast.LENGTH_SHORT).show();
return;
}
DefaultClient client = ServiceGenerator.createClient(DefaultClient.class);
Call<SignInModel> signInCall = client.signIn(
email,
password
);
signInCall.enqueue(new Callback<SignInModel>() {
@Override
public void onResponse(Call<SignInModel> call, Response<SignInModel> response) {
SignInModel signInModel = response.body();
DriverVehicleModel driverVehicleModel = signInModel.getDriverVehicleModel();
AgencyModel agencyModel = signInModel.getAgencyModel();
Intent intent = new Intent(LoginActivity.this, DetailActivity.class);
Bundle bundle = new Bundle();
SharedPreferences.Editor editor = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, MODE_PRIVATE).edit();
if (agencyModel != null) {
// agency
editor.putInt(AppConstants.KEY_ID, agencyModel.getId());
editor.putInt(AppConstants.KEY_TYPE, 0);
editor.apply();
bundle.putInt(AppConstants.DATA_TO_SHOW, 0);
bundle.putInt(AppConstants.KEY_ID, agencyModel.getId());
intent.putExtra(AppConstants.KEY_DATA, bundle);
startActivity(intent);
}
else if (driverVehicleModel != null) {
// driver
editor.putInt(AppConstants.KEY_ID, driverVehicleModel.getVehicle().getId());
editor.putInt(AppConstants.KEY_TYPE, 1);
editor.apply();
bundle.putInt(AppConstants.DATA_TO_SHOW, 1);
bundle.putInt(AppConstants.KEY_ID, driverVehicleModel.getVehicle().getId());
intent.putExtra(AppConstants.KEY_DATA, bundle);
startActivity(intent);
}
else {
Toast.makeText(LoginActivity.this, "Invailid username or password", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<SignInModel> call, Throwable t) {
Log.d(TAG, t.getMessage());
Toast.makeText(LoginActivity.this, "Failed to connect. Please check the internet connection" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
所有其他模型都是由服务器生成响应的。
出了什么问题以及如何解决。我已经成功地浪费了超过1天的时间。请帮忙。