我不知道我的问题在哪里。我要贴出原始的身体
{"app_id":"abced","app_key":"abced","request":"login","username":"abce@gmail.com","password":"1234"}我使用的是改造2.0。我创建了一个界面
@FormUrlEncoded
@POST("index.php/")
Call<LoginResponse> getHome(@Field("app_id") String request,
@Field("app_key") String request1,
@Field("request") String request2,
@Field("username") String request3,
@Field("password") String request4);像这样打电话:
Retrofit retrofitget = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
MyAPI ApiData = retrofitget.create(MyAPI.class);
Call<LoginResponse> GalleryGetSet = ApiData.getHome("abced","abced","login","abce@gmail.com","1234");
GalleryGetSet.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e(TAG, "data1" + response.body().getError());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e(TAG, "Error2" + t.getMessage());
}
});总是会出错:在第1列的输入端,是通过修改post发布原始正文数据的正确过程,还是我错了。已经在谷歌上搜索了,但没有得到我的解决方案。请大家帮帮我。
我还以这种方式在接口中尝试了
@FormUrlEncoded
@POST("index.php")
Call<LoginResponse> getHome1(@Field("login") String data);在打电话的时候
Retrofit retrofitget = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
MyAPI ApiData = retrofitget.create(MyAPI.class);
Call<LoginResponse> listGalleryGetSet = ApiData.getHome("ABCD","ABCD","login","ABC@gmail.com","1234");
listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e(TAG, "data1" + response.body().getError());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e(TAG, "Error" + t.getMessage());
}
});我的LoginResponse课程
public class LoginResponse {
private String error;
private Boolean vlink;
/**
*
* @return
* The error
*/
public String getError() {
return error;
}
/**
*
* @param error
* The error
*/
public void setError(String error) {
this.error = error;
}
/**
*
* @return
* The vlink
*/
public Boolean getVlink() {
return vlink;
}
/**
*
* @param vlink
* The vlink
*/
public void setVlink(Boolean vlink) {
this.vlink = vlink;
}}

依赖关系
compile 'com.squareup.retrofit2:retrofit-converters:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'预先感谢
发布于 2016-06-07 11:30:39
而不是使用
@POST("index.php")请试着用
@POST("/index.php")并将其作为
Call<LoginResponse> getHome(@Body YourBodyPojo pojo);这肯定对你有帮助。
发布于 2016-05-23 13:21:41
将示例正文Json粘贴到此站点,选择源Json,注释None:Json模式2 Pojo
然后在你的阿皮里:
@POST("index.php")
public Call<LoginResponse> getHome(@Body YourBodyPojo pojo);然后打电话:
OkHttpClient okClient = new OkHttpClient.Builder()
.connectTimeout(5,TimeUnit.SECONDS)
.build();
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okClient)
.build();
MyAPI apiData = retrofitget.create(MyAPI.class);
LoginRequest loginRequest=new LoginRequest();
loginRequest.setUserName("abc");
loginRequest.setPassword("123");
Call<LoginResponse> listGalleryGetSet = apiData.getHome(loginRequest);
listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.e(TAG, "data1" + response.body().getError());
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e(TAG, "Error" + t.getMessage());
}
});反应班:
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class YourResponse{
@SerializedName("error")
@Expose
private String error;
@SerializedName("vlink")
@Expose
private Boolean vlink;
/**
*
* @return
* The error
*/
public String getError() {
return error;
}
/**
*
* @param error
* The error
*/
public void setError(String error) {
this.error = error;
}
/**
*
* @return
* The vlink
*/
public Boolean getVlink() {
return vlink;
}
/**
*
* @param vlink
* The vlink
*/
public void setVlink(Boolean vlink) {
this.vlink = vlink;
}
}身体类别:
package com.example;
public class YourBodyClass{
private String appId;
private String appKey;
private String request;
private String username;
private String password;
/**
*
* @return
* The appId
*/
public String getAppId() {
return appId;
}
/**
*
* @param appId
* The app_id
*/
public void setAppId(String appId) {
this.appId = appId;
}
/**
*
* @return
* The appKey
*/
public String getAppKey() {
return appKey;
}
/**
*
* @param appKey
* The app_key
*/
public void setAppKey(String appKey) {
this.appKey = appKey;
}
/**
*
* @return
* The request
*/
public String getRequest() {
return request;
}
/**
*
* @param request
* The request
*/
public void setRequest(String request) {
this.request = request;
}
/**
*
* @return
* The username
*/
public String getUsername() {
return username;
}
/**
*
* @param username
* The username
*/
public void setUsername(String username) {
this.username = username;
}
/**
*
* @return
* The password
*/
public String getPassword() {
return password;
}
/**
*
* @param password
* The password
*/
public void setPassword(String password) {
this.password = password;
}
}发布于 2018-08-07 11:17:42
只是重新发布了一个对我有用的答案。
@Override
public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
if (response.isSuccessful()) {
//Do something if response is ok
} else {
JsonParser parser = new JsonParser();
JsonElement mJson = null;
try {
mJson = parser.parse(response.errorBody().string());
Gson gson = new Gson();
MyError errorResponse = gson.fromJson(mJson, MyError.class);
} catch (IOException ex) {
ex.printStackTrace();
}
}https://stackoverflow.com/questions/37303964
复制相似问题