简体   繁体   中英

Retrofit returns Http code 400

Update There seems to be an issue with the way dates are interpreted by retrofit and spring. Request works when date is set to null.


I am trying to send a POST request from android using retrofit2 to my backend server running on Spring. I get a 400 Bad Request code and can't find the reason for it. I tried the spring controller with swagger UI and it worked ok, so I'm guessing the android side is the issue.

Android side:

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

And spring side:

@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

Please enlighten me.

The Web server (running the Web site) thinks that the data stream sent by the client (eg your Web browser or our CheckUpDown robot) was 'malformed' ie did not respect the HTTP protocol completely. So the Web server was unable to understand the request and process it.

I guess you pass not completely data to server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM