Retrofit进行post提交json数据

1:先看一看xutils3的提交代码

 

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

如下图:非常好用

 

 

 

 

String account = editText1.getText().toString();

    String password = editText2.getText().toString();

    JSONObject js_request = new JSONObject();//服务器需要传参的json对象

    try {

        js_request.put("account", account);//添加相应键值对

        js_request.put("password", password);

    catch (JSONException e) {

        e.printStackTrace();

    }

    RequestParams requestParams = new RequestParams(LOGIN_URL);

    requestParams.setAsJsonContent(true);

    requestParams.setBodyContent(js_request.toString());

    x.http().post(requestParams, new Callback.CommonCallback<String>() {

        @Override

        public void onSuccess(String result) {

            System.out.println("**ok"+result);

            try {

                JSONObject object = new JSONObject(result);

                String code = object.getString("code");

                if (code.equals("1")) {

                    // button.setClickable(false);

                    //登录成功后获得id

                 

                else {

                    // 登陆失败

                }

 

            catch (Exception e) {

                e.printStackTrace();

            }

        }

        @Override

        public void onError(Throwable ex, boolean isOnCallback) {

            System.out.println("errot");

        }

 

        @Override

        public void onCancelled(CancelledException cex) {

        }

        @Override

        public void onFinished() {

        }

    });

}

   2:Retrofit提交过程

  2.1 登陆 urL 

1

public static String LOGIN_URL = "http://114.xx.xxx.xx:8088/vdyweb/ws/rest/Login";

  

1

2

3

4

interface APIStore {

   @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头

   @POST ("vdyweb/ws/rest/Login")

   Call<ResponseBody>getMessage(@Body RequestBody info);   // 请求体味RequestBody 类型

1

}

 

1

2

3

4

5

6

7

8

9

public class Info {

    String account;

    String password;

 

    public Info(String account, String password) {

        this.account = account;

        this.password = password;

    }

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

public class MainActivity extends AppCompatActivity {

    public static String BASE_LOGIN_URL = "http://114.xx.xxx.xx:8088/";

    Retrofit retrofit;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Info info=new Info("test","123456");   /*** 利用Gson 将对象转json字符串*/

        Gson gson=new Gson();

        String obj=gson.toJson(info);

        retrofit=new Retrofit.Builder().baseUrl(BASE_LOGIN_URL).build();

        RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),obj);

        final APIStore login = retrofit.create(APIStore.class);

        retrofit2.Call<ResponseBody> data = login.getMessage(body);

        data.enqueue(new Callback<ResponseBody>() {

            @Override

            public void onResponse(retrofit2.Call<ResponseBody> call, Response<ResponseBody> response) {

                Log.d(TAG, "onResponse: --ok--"+response.body());

                try {

                    Log.d(TAG, "onResponse: --ok--"+response.body().string());

                catch (IOException e) {

                    e.printStackTrace();

                }

            }

            @Override

            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

                Log.d(TAG, "onResponse: --err--"+t.toString());

            } });} }

3:添加get请求

  apiStore加

1

2

@GET("vdyweb/ws/rest/device/getOwnerDevice/2/2/20")

Call<ResponseBody>getMessage2();

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

retrofit2.Call<ResponseBody>data1=login.getMessage2();

       data1.enqueue(new Callback<ResponseBody>() {

           @Override

           public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

               try {

                   Log.d(TAG, "onResponse: --ok--"+response.body().string());

               catch (IOException e) {

                   e.printStackTrace();

               }

           }

           @Override

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

 

           }

       });

  

  

 

 

 

 

今天多一点积累,明天少一分烦恼

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值