android ——json参数post 请求

2017-08-19  本文已影响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();

}

}

});

}

上一篇下一篇

猜你喜欢

热点阅读