Retrofit返回Http代码400

时间:2016-08-21 17:34:00

标签: android spring retrofit2 bad-request

更新通过改造和弹簧解释日期的方式似乎存在问题。当date设置为null时,请求有效。

我正在尝试使用retrofit2将android发送到我在Spring上运行的后端服务器。我收到400 Bad Request代码,但无法找到原因。我尝试了带有swagger UI的弹簧控制器,它工作正常,所以我猜测android方面是问题所在。

Android方面:

 //this is inside Async task, there's 1 more retrofit call above
 UserTemplate template;
 // fill template
   UserService userService = (UserService) RetrofitBuilder.getService(UserService.class, 
                             RetrofitBuilder.BASE_URL);
        Call<User> userCall = userService.register(template);
        Response<User> registeredUser;
        try {
            registeredUser = userCall.execute();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        Log.d("call", "call status " + registeredUser.code());
        return registeredUser.body();
}
//---------------------------------------------------------

public class RetrofitBuilder {
public static final String BASE_URL = "http://my-backend-url.com/";
public static final String IMGUR_URL = "https://api.imgur.com/";

/**
 *   Added logging
 */
public static Retrofit build(String url) {
    GsonBuilder builder = new GsonBuilder();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });

    Gson gson = builder.create();
    return new Retrofit.Builder()
            .client(client)
            .baseUrl(useLocalhost ? LOCALHOST : url)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
}


public static Object getService(Class<?> clas, String url) {
    return build(url).create(clas);
}
}
//----------------------------------------------------
public interface UserService {

@POST("/register/")
Call<User> register(@Body UserTemplate userTemplate);
}

春天的一面:

@RestController
public class RegisterController {

@Autowired
UserService userService;

@RequestMapping(value = "register", method = RequestMethod.POST)
public ResponseEntity<?> register(@RequestBody UserTemplate userTemplate) {
    User user = UserMapper.map(userTemplate);
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    user.setPassword(passwordEncoder.encode(user.getPassword()));
    User u = userService.register(user);
    return ResponseEntity.ok().body(u);

}

UserTemplate

public class UserTemplate {

String firstName;
String lastName;
String email;
String username;
String password;
float targetDistance;
String notifications;
Date dateOfBirth;
String description;
String profilePicture;

//getters, setters, toString
}

OkHttp log

D/OkHttp: --> POST http://mafia-test-env-java.eu-central-1.elasticbeanstalk.com/register http/1.1
D/OkHttp: Content-Type: application/json
D/OkHttp: Content-Length: 168
D/OkHttp: {"dateOfBirth":"Aug 21, 2016 12:00:00 AM","email":"a@a.aa","firstName":"A","lastName":"A","password":"a","profilePicture":"IyYD8uY","username":"a","targetDistance":0.0}
D/OkHttp: --> END POST (168-byte body)
D/OkHttp: <-- 400 Bad Request http://mafia-test-env-java.eu-central-1.elasticbeanstalk.com/register (192ms)
D/OkHttp: Server: nginx/1.8.1
D/OkHttp: Date: Sun, 21 Aug 2016 20:28:02 GMT
D/OkHttp: Content-Type: application/json;charset=UTF-8
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Connection: keep-alive
D/OkHttp: Access-Control-Allow-Origin: *
D/OkHttp: Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, DELETE
D/OkHttp: Access-Control-Max-Age: 3600
D/OkHttp: Access-Control-Allow-Headers: Origin, X-Requested-With, Content-     Type, Accept, ${cerberus.token.header}
D/OkHttp: X-Content-Type-Options: nosniff
D/OkHttp: X-XSS-Protection: 1; mode=block
D/OkHttp: Cache-Control: no-cache, no-store, max-age=0, must-revalidate
D/OkHttp: Pragma: no-cache
D/OkHttp: Expires: 0
D/OkHttp: X-Frame-Options: DENY
D/OkHttp: <-- END HTTP (binary 1408-byte body omitted)


D/call: call status =400, message =Bad Request

请赐教。

1 个答案:

答案 0 :(得分:1)

Web服务器(运行Web站点)认为客户端(例如您的Web浏览器或我们的CheckUpDown机器人)发送的数据流格式错误&#39;即完全不尊重HTTP协议。因此,Web服务器无法理解请求并处理它。

我猜你没有把完整的数据传递给服务器。