美文网首页
android ——json参数post 请求

android ——json参数post 请求

作者: 未聞椛洺 | 来源:发表于2017-08-19 12:53 被阅读0次

    public static finalMediaTypeJSON= MediaType.parse("application/json; charset=utf-8");

    private voidmodifyNickName(String newNickName) {

    showProgressDialog(R.string.loading);

    OkHttpClient  client =newOkHttpClient();

    JSONObject jsonObject =newJSONObject();

    try{

    jsonObject.put("uid",MeManager.getUid());

    jsonObject.put("token",MeManager.getToken());

    jsonObject.put("nick_name",newNickName);

    }catch(JSONException e) {

    e.printStackTrace();

    }

    RequestBody body = RequestBody.create(JSON,String.valueOf(jsonObject));

    Request request =newRequest.Builder()

    .url(HOST+NICKNAME_CHANGE)

    .post(body)

    .build();

    client.newCall(request).enqueue(newCallback() {

    @Override

    public voidonFailure(Call call,IOException e) {

    ToastUtils.showToast(R.string.send_faid);

    }

    @Override

    public voidonResponse(Call call,Response response)throwsIOException {

    String result = response.body().string();

    try{

    JSONObject jsonObject =newJSONObject(result);

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

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

    MeManager.clearName();

    MeManager.setName(newNickName);

    openActivity(PersonalInfoActivity.class);

    runOnUiThread(newRunnable() {

    @Override

    public voidrun() {

    ToastUtils.showToast(R.string.save_success);

    }

    });

    finish();

    }

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

    String resultError = jsonObject.getString("message");

    runOnUiThread(newRunnable() {

    @Override

    public voidrun() {

    ToastUtils.showToast(resultError);

    }

    });

    }

    }catch(JSONException e) {

    e.printStackTrace();

    }

    }

    });

    }

    相关文章

      网友评论

          本文标题:android ——json参数post 请求

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