Retrofit进行post提交json数据

  • Post author:
  • Post category:其他


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) {




}




});

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



版权声明:本文为u014644594原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。