private AsyncHttpClient asyncHttpClient; private Dialog dialog; /** * 改变简历状态 */ private void postchangestate(final boolean mIsOpen,final boolean mIsLoader) { asyncHttpClient = new AsyncHttpClient(); RequestParams rp = new RequestParams(); if(mIsLoader){ rp.put("islook", ""); }else{ rp.put("islook", mIsOpen?"1":"0"); } rp.put("user_openid", getAppContext().getAppUserOpenId()); rp.put("client_id", getAppContext().getAppClientId()); rp.put("client_sign", MD5Util.string2MD5(Unity.appkey + getAppContext().getAppClientId() + getAppContext().getAppUserOpenId())); asyncHttpClient.setTimeout(6 * 10 * 1000); asyncHttpClient.post(this, Unity.changeresumestate, rp, new AsyncHttpResponseHandler() { @Override public void onStart() { if(mIsLoader){ changeLoadState(true); }else{ dialog = new MyDialog(TalentedChangeStateActivity.this).showProgressDialog(asyncHttpClient); } super.onStart(); } @Override public void onSuccess(String content) { if(mIsLoader){ changeLoadState(false); }else{ if (dialog != null) dialog.dismiss(); } Gson gson = new GsonBuilder().create(); try { String stats = new JSONObject(content).optString("stats"); if ("true".equals(stats)) { mToggleButton.setChecked(true); state_tv.setText("NO"); } else { mToggleButton.setChecked(false); state_tv.setText("OFF"); } } catch (Exception e) { AppErrorModel appErrorModel=gson.fromJson(content, AppErrorModel.class); eesycard_show_choose_loadbar.setVisibility(View.GONE); eesycard_show_choose_loadtv.setVisibility(View.VISIBLE); eesycard_show_choose_loadtv.setText(appErrorModel.getError_description()); mMessageBar.show(appErrorModel.getError_description()); } super.onSuccess(content); } @Override public void onFailure(Throwable error) { if (dialog != null) dialog.dismiss(); // 网络中断错误详情显示 if (error instanceof UnknownHostException) { mMessageBar.show(getResources().getString(R.string.load_no_network_error)); } else if (error instanceof HttpResponseException) { // 请求错误详情 mMessageBar.show(getResources().getString(R.string.load_http_response_error)); } else if (error instanceof SocketTimeoutException) { // 请求超时详情 mMessageBar.show(getResources().getString(R.string.load_socket_timeout_error)); } eesycard_show_choose_loadbar.setVisibility(View.GONE); eesycard_show_choose_loadtv.setVisibility(View.GONE); reloadBtn.setVisibility(View.VISIBLE); reloadBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { postchangestate(false,true); } }); super.onFailure(error); } }); }
private AppContext getAppContext() { return (AppContext) getApplicationContext(); }
/** * 初始化加载更新的UI */ public void changeLoadState(boolean flag) { reloadBtn.setVisibility(View.GONE); eesycard_show_choose_loadbar.setVisibility(flag ? View.VISIBLE : View.GONE); content_view.setVisibility(flag ? View.GONE : View.VISIBLE); }
@Override protected void onDestroy() { if (asyncHttpClient != null) asyncHttpClient.cancelRequests(this, true); super.onDestroy(); }
//这是调用 mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub postchangestate(isChecked,false); } }); postchangestate(false,true);