0

当我尝试使用我的 restTemplate.postForEntity 时,我收到 404 bad request 我的错是什么?

代码:

服务器调用者:

public ServerError login(String username, String password) {
       User user = new User();
       user.setUsername(username);
       user.setPassword(password);

       try {
           String URL = "http://"+ipAddress+"/ProjectTeamF-1.0/service/login.json";
           Object[] params = new Object[]{URL,user};
           new login().execute(params);


       } catch (ResourceAccessException rae) {
           receivedUser = null;
           return ServerError.ServerNotFound;
       } catch (HttpServerErrorException hsee) {
           receivedUser = null;
           return ServerError.WrongData;
       } catch(RestClientException rce){
           receivedUser = null;
           return ServerError.WrongData;
       } catch (Exception e) {
           System.out.println("error " + e);
           receivedUser = null;
           return ServerError.OtherError;
       }
       return ServerError.NoError;
   }

异步任务:

public class login extends AsyncTask<Object[], Void, Void> {
public ServerCaller sc = ServerCaller.getInstance();
public User body;

@Override
protected Void doInBackground(Object[]... params) {
    String message = "";
    try {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<User> _entity = new HttpEntity<User>((User)params[0][1], requestHeaders);
        RestTemplate templ = new RestTemplate();
        templ.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        templ.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
        ResponseEntity<User> _response = templ.postForEntity(params[0][0].toString(), _entity, User.class); //null here in order there wasn't http converter errors because response type String and [text/html] for JSON are not compatible;
        body = _response.getBody();
        return null;

    } catch (Exception e) {
        message = e.getMessage();
        return null;
    }
}

@Override
   protected void onPostExecute(Void aVoid) {
   super.onPostExecute(aVoid);
   sc.setReceivedUser(body);
}
}

当我在 templ.postForEntity() 调试程序时崩溃。该错误告诉我这是一个 400 - 错误请求。

谢谢!

4

0 回答 0