HTTP post 400错误请求

时间:2013-03-07 13:44:28

标签: android post android-asynctask

当我尝试使用我的restTemplate.postForEntity时,我收到了404错误请求 我的错是什么?

代码:

Servercaller:

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;
   }

的AsyncTask:

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 - 不好的请求。

谢谢!

0 个答案:

没有答案