Retrofit2:使用String请求发送POST请求

时间:2016-11-26 09:10:48

标签: android retrofit2

您好我是Android新手,现在我正在使用Retrofit来集成Web服务。

我不了解如何使用Retrofit POST请求将参数发送到服务器。

请帮帮我。感谢。

MainActivity: -

 String url = "XXXXXXXXX/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).
                addConverterFactory(GsonConverterFactory.create()).build();

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

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("email", "device3@gmail.com");
            jsonObject.put("password", "1234");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final String result = jsonObject.toString();

        service.getStringScalar(result).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                System.out.println("result is====>" + response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                System.out.println("Failuere");
            }
        });
    }

PostInterface: -

public interface PostInterface {

    @POST("User/DoctorLogin")
    Call<String> getStringScalar(@Body String body);
}

4 个答案:

答案 0 :(得分:1)

制作一个新的模型类:

public class Credentials {
    private String email;
    private String password;
    public Credentials(String email, String password) {
        this.email = email;
        this.password = password;
    }
}

然后修改你的界面:

public interface PostInterface {

    @POST("User/DoctorLogin")
    Call<String> getStringScalar(@Body Credentials body);
}

然后这样称呼:

service.getStringScalar(new Credentials("device3@gmail.com", "1234")).enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {

            System.out.println("result is====>" + response.body());
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            System.out.println("Failuere");
        }
    });

答案 1 :(得分:0)

public interface PostInterface {

@POST("User/DoctorLogin")
Call<JsonObject> getStringScalar(@Field("PARAMATER_ID") String body);}

您也可以使用字符串代替jsonobject,但我会建议使用json。 &#34; @Field(YOUR_PARAMETER)&#34;是你使用这个

的方式

答案 2 :(得分:0)

阅读this artical这有助于您了解改造。

答案 3 :(得分:0)

创建一个模型类

public class Model {

String userName;
String password;

}

生成getter和setter

修改您的界面

public interface ApiInterface {
@FormUrlEncoded
@POST("User/DoctorLogin")
Call<Model> loadDetail(@Field("user_id") String user_id,
                       @Field("password") String password);
}

并像这样称呼它

 Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create()).baseUrl(url)
            .build();

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

    Call<Model> call = service.loadDetail("username", "password");
    call.enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {

            int statusCode = response.code();
            Model user = response.body();

            Log.d("userName: "  user.getUserName());
           Log.d("password: " + user.getPassword());
        }

        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "data not found", Toast.LENGTH_SHORT).show();
        }
相关问题