由于这类问题已被问过很多次,但我无法弄清楚,在我的案例中发生了什么。我在spring boot中创建了一个服务。虽然在postman中测试它工作正常,但是当我在AsynchTask的android中尝试同样的事情时,它会抛出异常。任何人都可以帮我弄清楚这个问题。感谢
以下是春季启动时的服务
@RequestMapping(path="/sellerRegistration", method = RequestMethod.POST,
consumes = "application/json")
public String doSellerRegistration(@RequestBody SellerModel sModel) {
int insertCount = uDao.sellerRegistrations(sModel.getShopName(), sModel.getLocalAreaName(), sModel.getAddress(), sModel.getMobileNo(), sModel.getPassword());
System.out.println("inside seller registration");
if (insertCount > 0) {
return "1";
} else {
return "0";
}
}
然后我在Android中创建了一个方法,并从我的活动中从AsynchTask doInBackground()方法调用此方法。
public static String getJSONResponse(String url, String postData){
String result = "";
BufferedReader reader = null;
StringBuilder stringBuilder;
Log.i("Rest url..", url);
Log.i("Rest postData..", postData);
try{
URL myURL = new URL(url);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/json");
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
Log.i("Rest myURLConnection..", ""+myURLConnection);
DataOutputStream wr = new DataOutputStream (myURLConnection.getOutputStream ());
wr.writeBytes (postData);
wr.flush ();
wr.close ();
reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
result = stringBuilder.toString();
Log.i("REST result....", ""+result);
} catch (Exception e) {
Log.e("Rest service Task", e.getMessage(), e);
}
return result;
}
以下是我打电话的代码
@Override
protected String doInBackground(String... strings) {
String shopName = "SomeShopName";
String url = "http://192.168.43.166:2020/OnlineGroceryMarket/users/sellerRegistration?shopName="+shopName;
String response = GroceryRestService.getJSONResponse(url, "");
return response;
}
在弹簧启动的情况下,这就像下面一样向我抛出错误,但是当在邮递员中调用它时,它的工作非常好。
2018-05-06 12:38:31.102 WARN 8204 --- [nio-2020-exec-1]
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.mangal.grocerymarket.user.controller.UsersController.doSellerRegistration(com.mangal.grocerymarket.user.model.SellerModel)
2018-05-06 12:38:31.102 WARN 8204 --- [nio-2020-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.mangal.grocerymarket.user.controller.UsersController.doSellerRegistration(com.mangal.grocerymarket.user.model.SellerModel)
答案 0 :(得分:0)
您应该在写入postData
之前对DataOutputStream
进行编码,并且还应该更新以下行
myURLConnection.setRequestProperty("Content-Type", "application/json");
用这个
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
如果不起作用,请按以下步骤更新您的网址
http://192.168.43.166:2020/OnlineGroceryMarket/users/sellerRegistration/
答案 1 :(得分:0)
也许尝试改变
wr.writeBytes (postData);
到
wr.writeBytes (postData.getBytes("UTF-8"));