通过 Retrofit 请求 open API

Request through Retrofit like open API

我想构建 POST 像打开 API 我尝试的那样的请求 use.But 我有错误,错误的请求,代码==400.What 我做错了并且如何建立正确的要求? API 中的此请求示例:

curl -X POST
     -H "Authorization: Basic Zm9vOmJhcg=="
     -d "username=foo&password=bar" https://api.vid.me/auth/create

我的界面class:

public interface VideoApi {

    @GET("/videos/featured")
    Call<Videos> getFeaturedVideo();

    @GET("/videos/new")
    Call<Videos> getNewVideo();

@Headers("Content-Type:application/x-www-form-urlencoded")
    @FormUrlEncoded
    @POST("/auth/create")
   Call<SignInResults>insertUser(@Header("Authorization") String authorization,@Field("username") String username,
                           @Field("password") String password
                           );
}

片段:

public class FeedFragment extends Fragment {
    EditText username;
    EditText password;
    Button btnLogin;

    public List<SignInResult> signInResult;
    String username_value,password_value;
    public static final String ROOT_URL = "https://api.vid.me/";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
        username = (EditText) rootView.findViewById(R.id.user_name_field);
        password = (EditText) rootView.findViewById(R.id.password_field);
        btnLogin = (Button) rootView.findViewById(R.id.button_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Authorize();
            }
        });
        return rootView;
    }

    public void Authorize() {
        Retrofit retrofitAdapter = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ROOT_URL)
                .build();
        final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);

         username_value = username.getText().toString();
         password_value = password.getText().toString();
String basicauth = "Basic "+ Base64.encodeToString(String.format("%s:%s",username_value,password_value).getBytes(),Base64.NO_WRAP);
        Call<SignInResults> call = videoApi.insertUser(basicauth,username_value,password_value);
        call.enqueue(new Callback<SignInResults>() {


            @Override
            public void onResponse(Call<SignInResults> call, Response<SignInResults> response) {
                SignInResults results = response.body();
                Log.d("Response ==>> ", new GsonBuilder().setPrettyPrinting().create().toJson(results));

            }

            @Override
            public void onFailure(Call<SignInResults> call, Throwable t) {

            }
        });
}
}

堆栈跟踪:

rawResponse = {Response@830026791184} "Response{protocol=http/1.1, code=400, message=Bad Request, url=https://api.vid.me/auth/create}"
 body = {OkHttpCall$NoContentResponseBody@830025624336} 
 cacheControl = null
 cacheResponse = null
 request = {Request@830024019616} "Request{method=POST, url=https://api.vid.me/auth/create, tag=Request{method=POST, url=https://api.vid.me/auth/create, tag=null}}"
 handshake = {Handshake@830025630784} 
 headers = {Headers@830025578160} "Content-Type: application/json\nDate: Tue, 26 Apr 2016 07:36:04 GMT\nServer: nginx\nSet-Cookie: rid=58132ed5dd9a405f851d7ba3a05238e5; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475092750; path=/; domain=vid.me\nSet-Cookie: srid=kKmWgypTQSSDdiKprKNUdg-DQiaXQ-IrRIa9riWD48kQG4bK4Mzfbu0fk; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475092750; path=/; domain=vid.me\nX-Request-Time: 12\nX-Vidme-Authorization-Okay: false\nX-Vidme-Server-Id: 79dce5c05de58beae1b751040fd8bcd0\nContent-Length: 85\nConnection: keep-alive\nOkHttp-Sent-Millis: 1461638110857\nOkHttp-Received-Millis: 1461638111114\n"
 message = "Bad Request"
 networkResponse = {Response@830025231040} "Response{protocol=http/1.1, code=400, message=Bad Request, url=https://api.vid.me/auth/create}"
 priorResponse = null
 protocol = {Protocol@830023817448} "http/1.1"
 code = 400

我用 Postman 测试了你的请求,问题不在 Android,而是你的 API。错误是 "The password you entered was not valid" 为什么密码无效?

从邮递员那里看到这个屏幕: API Request

祝你有愉快的一天