美文网首页
Retrofit 2.0 解析JSON数据

Retrofit 2.0 解析JSON数据

作者: 随心者随心行 | 来源:发表于2018-04-24 08:41 被阅读0次

    Retrofit 2.0 解析数据


    @POST("/login")
    
    @FormUrlEncoded
    
    Calllogin(@Field("jobId") String jobId,@Field("pwd") String pwd);
    
    @POST("/orders/status")
    
    @FormUrlEncoded
    
    Calladminsearch(@Field("u") String u,@Field("status")intstatus);
    
    Retrofit 2.0怎样进行网络请求很多人都写了,我就不写了。我这里就贴我写的 请求之后如何用GSON解析JSON数据
    
    获取服务器返回的JSON数据之后用GSON解析.
    
    allOrderSearch.setOnClickListener(new View.OnClickListener() {
    
    @Override
    
    public void onClick(View v) {
    
      if (TextUtils.isEmpty(allOrderSearchUser.getText()) ) {
    
      Toast.makeText(AllOrdersActivity.this, "请输入要查询的相关工号", Toast.LENGTH_SHORT).show();
    
    } else {
    
    //        login();
    
      Gson gson = new GsonBuilder()
    
      //配置你的Gson
    
        .setDateFormat("yyyy-MM-dd hh:mm:ss")
    
        .create();
    
      Retrofit retrofit = new Retrofit.Builder()
    
    .baseUrl("http://10.1.1.1:8080") // 设置网络请求的Url地址
    
    .addConverterFactory(GsonConverterFactory.create())
    
    .build();
    
    String u = allOrderSearchUser.getText().toString().trim();
    
    int status=0;
    
    APi api = retrofit.create(APi.class);
    
    Callcall = api.adminsearch(u,status);
    
    //            call.enqueue(new Callback() {
    
    call.enqueue(new Callback() {
    
    @Override
    
    public void onResponse(Call call, Response response) {
    
    AdminOrderSearchBean adminOrderSearchBean = response.body();
    
    ArrayList adminBean=
    
    (ArrayList)adminOrderSearchBean
    
    .getData();
    
    Adminadapter = new AdminAdapter(adminBean);
    
    allOrderListview .setAdapter(Adminadapter);
    
    }
    
    @Override
    
    public void onFailure(Call call, Throwable t) {
    
      }
    
      });
    
    }
    
    }
    
    });
    
    /**
    
      * data :         {"jobId":"f1231","code":"0","bu":"AGBU","mail":"tab.x.li@mail.foxconn.com","org":"1","roleId":"7","userName":"鍛樺伐1","leaderId":"f123"}
    
    */
    
    @OnClick(R.id.main_btn_login)
    
    public void onClick() {
    
    if (TextUtils.isEmpty(username.getText()) && TextUtils.isEmpty(pwd.getText())) {
    
    Toast.makeText(this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
    
    } else {
    
      usernameLayout.setVisibility(View.INVISIBLE);
    
      passwordLayout.setVisibility(View.INVISIBLE);
    
    initAnim();
    
    //        login();
    
    Gson gson = new GsonBuilder()
    
    //配置你的Gson
    
    .setDateFormat("yyyy-MM-dd hh:mm:ss")
    
    . create();
    
    Retrofit retrofit = new Retrofit.Builder()
    
      .baseUrl("http://10.1.1.1:8080/") // 设置网络请求的Url地址
    
    .addConverterFactory(GsonConverterFactory.create())
    
      .build();
    
      String u = username.getText().toString().trim();
    
    String p = pwd.getText().toString().trim();
    
    APi api = retrofit.create(APi.class);
    
    Call call = api.login(u, p);
    
    //            call.enqueue(new Callback() {
    
    call.enqueue(new Callback() {
    
    @Override
    
    public void onResponse(Call call, Response response) {
    
    LoginBean loginBean = response.body();
    
    code = loginBean.getData().getCode();
    
    jobid = loginBean.getData().getJobId();
    
    role = loginBean.getData().getRoleId();
    
    }
    
    @Override
    
    public void onFailure(Call call, Throwable t) {
    
    }
    
      });
    
    }
    

    我是一个自学Android和JavaWeb的菜鸟。如果可以Flower一下我的GitHub。感激不尽

    https://github.com/FoxconnPeter

    相关文章

      网友评论

          本文标题:Retrofit 2.0 解析JSON数据

          本文链接:https://www.haomeiwen.com/subject/uhjdlftx.html