【发布时间】:2016-09-07 10:02:17
【问题描述】:
在我的 android 应用程序中,我正在尝试使用改造来进行 api 调用。我想使用改造来执行用户注册。问题是,执行了 api 调用并且调试器也转到了onResponse(),但我的 api 返回响应消息为“不接受请求方法”。我咨询了邮递员,它工作正常。在邮递员中,我将值作为表单数据传递。请帮我解决这个问题。这是我的代码:
public interface RegisterAPI {
@FormUrlEncoded
@POST("Register.php")
Call<RegisterPojo> insertUser(
@Field("username") String username,
@Field("email") String email,
@Field("password") String password,
@Field("c_password") String c_password
);
}
活动
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RegisterAPI service = retrofit.create(RegisterAPI.class);
Call<RegisterPojo> call = service.insertUser(
edtUname.getText().toString(), edtEmail.getText().toString(),
edtPassword.getText().toString(), edtConfirmPassword.getText().toString());
call.enqueue(new Callback<RegisterPojo>() {
@Override
public void onResponse(Call<RegisterPojo> call, Response<RegisterPojo> response) {
if (response.body() != null) {
//HERE IT SHOWS "Request method not accepted"
Log.e("msg", response.body().getMsg());
Toast.makeText(mContext, response.body().getMsg(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<RegisterPojo> call, Throwable t) {
Log.e("msg", "Failed");
}
});
注册Pojo
public class RegisterPojo {
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("msg")
@Expose
private String msg;
@SerializedName("id")
@Expose
private String id;
@SerializedName("email")
@Expose
private String email;
@SerializedName("username")
@Expose
private String username;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "app.sample"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
testCompile 'junit:junit:4.12'
compile 'com.github.aakira:expandable-layout:1.5.1@aar'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
// compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.google.code.gson:gson:2.7'
}
请告诉我哪里出错了。
【问题讨论】:
-
把设备日志放在这里
-
你的 api 可能只接受 GET 方法。
-
不,它接受 POST 方法。我在邮递员中使用 POST 方法执行了相同的 api。它与它完美配合。
-
从@POST("Register.php")中删除.php
-
并像这样使用
@POST("/Register")......并且还编辑你的logcat错误问题......
标签: android retrofit retrofit2 okhttp3 android-webservice