改型2(Android)-如何通过POST方法发送表单数据
*注意:更改了安全事项的参考资料 我试图从服务器获取json,但它需要请求体中的表单数据,如下所示: 在postman中进行测试时,它可以工作,但我就是不知道如何使用2来实现这一点 豆子:改型2(Android)-如何通过POST方法发送表单数据,android,request,http-post,retrofit,retrofit2,Android,Request,Http Post,Retrofit,Retrofit2,*注意:更改了安全事项的参考资料 我试图从服务器获取json,但它需要请求体中的表单数据,如下所示: 在postman中进行测试时,它可以工作,但我就是不知道如何使用2来实现这一点 豆子: public class Y { @SerializedName("id") private int yId; @SerializedName("name") private String yName; //... } public class YList { @SerializedName("
public class Y {
@SerializedName("id")
private int yId;
@SerializedName("name")
private String yName;
//...
}
public class YList {
@SerializedName("ys")
private List<Y> ys;
//...
}
我设法找到了一个解决办法,结果比我想象的要容易得多 只需像这样包装一个名为AuthRequest的类:
public interface YService {
@POST("y")
Call<YList> getY()
public static final YService serviceY = new Retrofit.Builder()
.baseUrl("http://example.com.br/api/x/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(YService.class);
}
public class AuthRequest {
private static final String TAG = "Auth";
private static final String EMAIL = "email";
private static final String PASSWORD = "pass";
//creates a json-format string
public static String createAuthJsonString(){
String info = "{\"auth\":{\"email\":\""+EMAIL+"\",\"password\":\""+PASSWORD+"\"}}";
Log.i(TAG,info);
return info;
}
}
只需将@FormUrlEncoded添加到@Post,并将@Field键和值放入方法调用中:
@FormUrlEncoded
@POST("ys")//endpoint
Call<YList> getY(@Field("info") String info);
// Connection url.
public static final YService serviceY = new Retrofit.Builder()
.baseUrl("http://example.com.br/api/x/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(YService.class);
@FormUrlEncoded
@POST(“ys”)//端点
调用getY(@Field(“info”)字符串信息);
//连接url。
public static final YService serviceY=新改型.Builder()
.baseUrl(“http://example.com.br/api/x/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(YService.class);
使用getY()方法:
final YService YService=YService.serviceY;
//...
getY(AuthRequest.createAuthJsonString()).enqueue(新
回调函数(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
//...
}否则{
//...
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//...
}
});
}
从json完美地返回了YList。
@FormUrlEncoded
@POST(“xxx”)//端点
调用getxxx(@Field(“phone”)字符串电话);
您的界面在哪里?你的电话在哪里?添加更多信息代码。希望有人能帮助我@KostasDrakdid您读过关于@Body改装注释的内容吗?我尝试使用getY(@Body AuthRequest.Auth info)。其中AuthRequest有一个带有电子邮件和密码属性的“Auth”内部类,但该类不起作用。我猜这是因为没有对表单数据键名称的引用@科斯塔斯德拉克
public class AuthRequest {
private static final String TAG = "Auth";
private static final String EMAIL = "email";
private static final String PASSWORD = "pass";
//creates a json-format string
public static String createAuthJsonString(){
String info = "{\"auth\":{\"email\":\""+EMAIL+"\",\"password\":\""+PASSWORD+"\"}}";
Log.i(TAG,info);
return info;
}
}
@FormUrlEncoded
@POST("ys")//endpoint
Call<YList> getY(@Field("info") String info);
// Connection url.
public static final YService serviceY = new Retrofit.Builder()
.baseUrl("http://example.com.br/api/x/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(YService.class);
final YService yService = YService.serviceY;
//...
yService.getY(AuthRequest.createAuthJsonString()).enqueue(new
Callback<YList>() {
@Override
public void onResponse(Call<YList> call, Response<YList> response) {
if (response.isSuccessful()) {
//...
} else {
//...
}
}
@Override
public void onFailure(Call<YList> call, Throwable t) {
//...
}
});
}
@FormUrlEncoded
@POST("xxx")//endpoint
Call<xxxx> getxxx(@Field("phone") String phone);