Retrofit进行post提交json数据

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

 

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

如下图:非常好用

Retrofit进行post提交json数据_第1张图片

 

Retrofit进行post提交json数据_第2张图片

 

 

 

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

        @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")

   CallgetMessage(@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 data = login.getMessage(body);

        data.enqueue(new Callback() {

            @Override

            public void onResponse(retrofit2.Call call, Response 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 call, Throwable t) {

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

            } });} }

3:添加get请求

  apiStore加

1

2

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

CallgetMessage2();

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

retrofit2.Calldata1=login.getMessage2();

       data1.enqueue(new Callback() {

           @Override

           public void onResponse(Call call, Response response) {

               try {

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

               catch (IOException e) {

                   e.printStackTrace();

               }

           }

           @Override

           public void onFailure(Call call, Throwable t) {

 

           }

       });

  

  

 

 

 

 

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

你可能感兴趣的:(retrofit)