简体   繁体   English

使用参数改造后的请求

[英]Retrofit post request with parameters

I am using postman extension for post request. 我正在使用邮递员扩展名进行后期处理。 I want to make same request with android. 我想用android发出相同的请求。 I used retrofit library for access my goal. 我使用改造库来访问我的目标。 But I can't get successful result. 但是我无法获得成功的结果。 Where is my mistake in code ? 我的代码错误在哪里?

Postman : 邮递员: 在此处输入图片说明

My interface : 我的界面:

public interface Interfacem {

    @FormUrlEncoded
    @POST("/login")
    Call<ResponseBody> getResponse(@Field("signin[username]") String username,@Field("signin[password]")String password );
}

and retrofit usage : 和改造用途:

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com")
                        .build();

                Interfacem service = retrofit.create(Interfacem.class);
                Call<ResponseBody> result =service.getResponse("myUsername","myPassword");
                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            System.out.println(response.body().string().toString());
                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });

Try this provide body 试试这个提供身体

public class SignBody {
  @SerializedName("signin[username]") @Expose private String username;
  @SerializedName("signin[password]") @Expose private String password;
}

change interface to 将界面更改为

@POST("/login")
    Call<ResponseBody> getResponse(@Body SignBody body);

if you are using Retrofit 2.x , Try to change your build of Retrofit object as below : 如果您使用的是Retrofit 2.x ,请尝试如下更改Retrofit对象的版本:

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .build();

maybe the things below will help 也许下面的事情会有所帮助

the leading / within the partial url overrides the end of API endpoint definition. 部分网址中的前导 /会覆盖API端点定义的末尾。 Removing the / from the partial url and adding it to the base url will bring the expected result. 从部分网址中删除/并将其添加到基本网址中将会带来预期的结果。

Example: 例:

The API interface API接口

    public interface UserService {  
    @POST("/me")
    Call<User> me();}

Build Retrofit with baseUrl 使用baseUrl Retrofit

    Retrofit retrofit = Retrofit.Builder()  
    .baseUrl("https://your.api.url/v2");
    .build();

Then Call : 然后致电:

UserService service = retrofit.create(UserService.class);

--> The request Url will be https://your.api.url/me ( /v2 has been disapear) ->请求网址将为https://your.api.url/me/v2https://your.api.url/me

PRO TIP 专家提示

use relative urls for your partial endpoint urls and end your base url with the trailing slash / . 使用相对URL作为部分终结点URL,并在基本URL末尾加上斜杠/

Interface 接口

public interface UserService {  
    @POST("me")
    Call<User>me();
}

Retrofit with baseURL 使用baseURL进行改造

Retrofit retrofit = Retrofit.Builder()  
    .baseUrl("https://your.api.url/v2/");
    .build();

Call 呼叫

UserService service = retrofit.create(UserService.class);

--> The request URL will be https://your.api.url/v2/me ->请求网址将为https://your.api.url/v2/me

Which version you use in retrofit? 您在翻新中使用哪个版本? Below code for version 2.0. 下面的代码为版本2.0。 If any Header require please check. 如果有任何标题,请检查。 If you can share url and username or password. 如果可以共享url和用户名或密码。 I will check. 我会检查。

public interface Interfacem {

    @FormUrlEncoded
    @POST("login")
    Call<ResponseBody> getResponse(@Field("username") String username,@Field("password")String password );
}


Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://myurl.com/")
                        .build();

                Interfacem service = retrofit.create(Interfacem.class);
                Call<ResponseBody> result =service.getResponse("myUsername","myPassword");
                result.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            System.out.println(response.body().string().toString());
                        } catch (IOException|NullPointerException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        t.printStackTrace();
                    }
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM