Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在安卓POST方法中随身体一起使用改装?_Java_Android_Retrofit_Retrofit2 - Fatal编程技术网

Java 如何在安卓POST方法中随身体一起使用改装?

Java 如何在安卓POST方法中随身体一起使用改装?,java,android,retrofit,retrofit2,Java,Android,Retrofit,Retrofit2,我有一个url,请求参数是JsonFormat,如 {“电子邮件地址”:user@gmail.com,“PassWord”:“PassWord”}它是请求的参数。 当我在《邮递员》中使用时,它是可以的。但当我用程序请求时,我得到了错误响应。我已经试过了,直到像这样请看这个片段 之后我使用了界面 公共接口服务{ @FormUrlEncoded @POST(“/json/syncreply/AuthenticateUserRequest?”) Call LoginService(@Field(“Em

我有一个url,请求参数是JsonFormat,如 {“电子邮件地址”:user@gmail.com,“PassWord”:“PassWord”}它是请求的参数。 当我在《邮递员》中使用时,它是可以的。但当我用程序请求时,我得到了错误响应。我已经试过了,直到像这样请看这个片段

之后我使用了界面

公共接口服务{
@FormUrlEncoded
@POST(“/json/syncreply/AuthenticateUserRequest?”)
Call LoginService(@Field(“EmailAddress”)字符串userName,@Field(“PassWord”)字符串userPass,Callback Callback);
之后,我通过活动调用这个接口方法

login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(input_username.getText().toString()!=null&&input_password.getText().toString()!=null
&&!input_username.getText().toString().isEmpty()&&!input_password.getText().toString().isEmpty()){
LoginModel loginCredentials=新的LoginModel();
loginCredentials.userName=”test@gmail.com";
loginCredentials.userPass=“密码”;
字符串请求=“{\”电子邮件地址\”:“raj@gmail.com\"," +
“\”密码\“:\”通过\“}”;
sendPost(loginCredentials);
}否则{
Toast.makeText(getApplicationContext(),“请输入有效的用户名和密码。”,Toast.LENGTH_LONG).show();
}
}
});
public void sendPost(登录模型名称){
Log.e(“TAG”、“| |”+name.userPass+“| |”+name.userName);
//mAPIService.savePost(name).enqueue(新回调(){
Call Call=mAPIService.LoginService(name.userName,name.userPass,new Callback()){
@凌驾
公共void onResponse(调用、响应){
Log.e(“TAG”,“RESPONSE”+“| |”+RESPONSE.body());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“TAG”,“FAILURE”+“| |”+t.getMessage());
}
});
}
提前谢谢。如有任何答案,将不胜感激。我的英语很好,请避免使用


首先,在您的rest客户端界面上,更改如下方法,而不是分别接收电子邮件和密码,只接收一个字符串数组列表:

@FormUrlEncoded
    @POST(WEBSERVICE_NAME)
    Call<ModelClass> methodName(
            @Field("parameters" + "[]") ArrayList<String> paramsArrayList            
        );
@FormUrlEncoded
@POST(网络服务名称)
调用方法名(
@字段(“参数”+“[]”)ArrayList paramsArrayList
);
现在,使用GSON库将模型类的arraylist转换为JSON字符串,如下所示

private ArrayList<String> getModelClassArrayinString(ArrayList<ModelClass> arrayList) {
        ArrayList<String> arrayListString = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            arrayListString.add(new Gson().toJson(arrayList.get(i)).toString());
        }

        return arrayListString;
    }
私有ArrayList GetModelClassArrayString(ArrayList ArrayList){
ArrayList ArrayList String=新的ArrayList();
对于(int i=0;i
所以你的最后一个电话是这样的:

Call<LoginResponse> call = mAPIService.LoginService(getModelClassArrayinString(arrayListofModelClass), new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

                Log.e("TAG" , "RESPONSE"+"||"+response.body());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {

                Log.e("TAG" , "FAILURE"+"||"+t.getMessage());

            }
        });
Call Call=mAPIService.LoginService(getModelClassArrayinString(arrayListofModelClass),新回调(){
@凌驾
公共void onResponse(调用、响应){
Log.e(“TAG”,“RESPONSE”+“| |”+RESPONSE.body());
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“TAG”,“FAILURE”+“| |”+t.getMessage());
}
});

Hey Rajan使用请求体传递Json

String request = "{\"EmailAddress\":\"raj@gmail.com\"," + "\"PassWord\":\"pass\"}";
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),request);

@Headers("Content-Type: application/json; charset=utf-8")
@POST("/json/syncreply/AuthenticateUserRequest")
Call<ResponseBody> AuthenticateUserRequest(@Body RequestBody body);

aCall.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                     if (response.isSuccessful()) {
                         ResponseBody responseBody = response.body();
                     }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
String请求=“{\”EmailAddress\:\”raj@gmail.com\“,”+“\”密码\“:\”通过\“}”;
RequestBody=RequestBody.create(okhttp3.MediaType.parse(“application/json;charset=utf-8”),请求);
@标题(“内容类型:application/json;字符集=utf-8”)
@POST(“/json/syncreply/AuthenticateUserRequest”)
调用AuthenticateUserRequest(@Body-RequestBody-Body);
aCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
ResponseBody ResponseBody=response.body();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});

检查此Hello@uday请查看我的编程步骤我做了什么,如果您发现任何错误,请告诉我谢谢。@Rajan您的API需要JSON,因此您需要在正文中传递数据以了解更多信息请参考此链接谢谢@Piyus Patel now cursor转到响应()。然后接受答案,以便其他人能够确定哪一个是正确的…ThanksHello@Piyush Patel我得到了onResponse(){}->Response{protocol=http/1.1,code=200,message=OK,url=?}所以我想要回复文本,所以我会得到Hello@Piyush Patel我没有得到回复数据,只有我得到的回复代码是200,但内容如何得到?
private ArrayList<String> getModelClassArrayinString(ArrayList<ModelClass> arrayList) {
        ArrayList<String> arrayListString = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            arrayListString.add(new Gson().toJson(arrayList.get(i)).toString());
        }

        return arrayListString;
    }
Call<LoginResponse> call = mAPIService.LoginService(getModelClassArrayinString(arrayListofModelClass), new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

                Log.e("TAG" , "RESPONSE"+"||"+response.body());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {

                Log.e("TAG" , "FAILURE"+"||"+t.getMessage());

            }
        });
String request = "{\"EmailAddress\":\"raj@gmail.com\"," + "\"PassWord\":\"pass\"}";
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),request);

@Headers("Content-Type: application/json; charset=utf-8")
@POST("/json/syncreply/AuthenticateUserRequest")
Call<ResponseBody> AuthenticateUserRequest(@Body RequestBody body);

aCall.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                     if (response.isSuccessful()) {
                         ResponseBody responseBody = response.body();
                     }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });