【发布时间】:2019-03-07 18:46:40
【问题描述】:
我正在尝试从 API 获取数据,作为初学者,我必须使用 post 方法用户名和密码我收到错误 400,因为我无法将数据传递到服务器 这是代码
API接口:
public interface APIInterface {
@POST("http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@Body LoginResponse login);
}
API客户端:
class APIClient {
public static final String BASE_URL = "http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
getter-setter:
public class LoginResponse {
@SerializedName("username")
public String UserId;
@SerializedName("FirstName")
public String FirstName;
@SerializedName("LastName")
public String LastName;
@SerializedName("ProfilePicture")
public String ProfilePicture;
@SerializedName("Password")
public String Password;
public LoginResponse(String UserId, String Password) {
this.UserId = UserId;
this.Password = Password;
}
我收到这样的错误
`Response{protocol=http/1.1, code=400, message=Bad Request, url=http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php}'
我做错的任何方向都会有所帮助 { “用户名”:“测试”, “密码”:“123456” } 我必须得到的样本输出
{"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011\/04\/25\",\"$320,800\"]...
活动方式:
private void loginRetrofit2Api(String userId, String password) {
final LoginResponse login = new LoginResponse(userId, password);
Call<LoginResponse> call1 = apiInterface.createUser(login);
call1.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e("Login", response.toString());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show();
call.cancel();
}
});
}
public boolean checkValidation() {
userId = et_Email.getText().toString();
password = et_Pass.getText().toString();
if (et_Email.getText().toString().trim().equals("")) {
CommonMethod.showAlert("UserId Cannot be left blank", MainActivity.this);
return false;
} else if (et_Pass.getText().toString().trim().equals("")) {
CommonMethod.showAlert("password Cannot be left blank", MainActivity.this);
return false;
}
return true;
}
CommonMethod 类:
public static boolean isNetworkAvailable(Context ctx) {
ConnectivityManager connectivityManager
= (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public static void showAlert(String message, Activity context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
try {
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】:
-
您是否尝试过对帖子 url 进行 url 编码?在发送请求之前尝试
URLEncoder.encode(BASE_URL, "UTF-8"); -
不,先生,但它发生了错误,因为用户名和密码没有发送到 api 我不认为那部分有任何错误。
-
您可以分享您提出请求的代码吗?
-
@S.Souza 我已经添加了几乎所有的代码
-
这个api有文档吗?如果有,可以分享一下吗?