我想像我尝试使用的开放 API 一样构建 POST 请求。但是我有错误,错误的请求,code==400。我做错了什么以及如何构建正确的请求? API 中的此请求示例:
curl -X POST
-H "Authorization: Basic Zm9vOmJhcg=="
-d "username=foo&password=bar" https://api.vid.me/auth/create
我的接口(interface)类:
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
);
}
fragment :
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 中。错误是“您输入的密码无效”那么为什么密码无效?
从 postman 那里看到这个屏幕: API Request
祝你今天愉快
关于android - 像开放API一样通过Retrofit请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858294/