• Retrofit进行post提交json数据


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

        
          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 

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

      

     interface APIStore {
    @Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头 @POST ("vdyweb/ws/rest/Login") Call<ResponseBody>getMessage(@Body RequestBody info); // 请求体味RequestBody 类型
    } 
    public class Info {
        String account;
        String password;
    
        public Info(String account, String password) {
            this.account = account;
            this.password = password;
        }
    }
    

     

    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加

        @GET("vdyweb/ws/rest/device/getOwnerDevice/2/2/20")
        Call<ResponseBody>getMessage2();
    

      

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

      

      

     

     

     

  • 相关阅读:
    win10下安装为知笔记的markdown插件
    最近一段时间的工作状态
    C++中的取余与取模
    来新项目后,最心虚的一个夜晚
    g++添加支持c++11的选项
    Linux ssh远程登陆方式:密码与公钥
    判断脚本加载完成
    解决ie6下最小高度问题
    display:inline-block的深入理解
    ff下button按钮上的文字垂直居中
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/7891807.html
Copyright © 2020-2023  润新知