弹窗依次是:
您没有修改权限1-------data
您没有修改权限0-------x
您没有修改权限1-------power
猜测原因:
可能是不同步导致的问题,因为,onResponse 里面修改Power的值,他是在 client.newCall 中执行的,而返回值值ispower的返回值,
也就是说可能在回调函数没执行玩,二函数已经返回了。
解决:
/*post异步请求: postDataWithParame*/ private int ispower() { power = 0; OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。 FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体 formBody.add("username","ansen");//传递键值对参数 formBody.add("password","123"); //传递键值对参数 Request request = new Request.Builder()//创建Request 对象。 .url("http://172.25.1.234:8080/DormitoryHelper/user/login") .post(formBody.build())//传递请求体 .build(); client.newCall(request).enqueue(new Callback() { //回调方法的使用与get异步请求相同。 @Override public void onFailure(Call call, IOException e) { rtdate = "请求失败"; } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){//回调的方法执行在子线程。 Log.d("kwwl","获取数据成功了"); Log.d("kwwl","response.code()=="+response.code()); String rt = response.body().string(); rtdate = rt; Log.d("kwwl","response.body().string()==" + rt); if(rt.equals("1")){ power = 1; //有权限 } else{ power = Integer.valueOf(rt); //无权限 } } } }); return power; } /*************提示框*******************/ private void showExitDialog(String num){ // String rt = String.valueOf(num); new AlertDialog.Builder(this) .setTitle("提示") .setMessage("您没有修改权限" + num) .setPositiveButton("确定", null) .show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { int x = 0; switch (seekBar.getId()) { case R.id.sb_data_uptime_h: x = ispower(); if(x == 1){ sendCommand(KEY_UPTIME_H, (seekBar.getProgress() + UPTIME_H_OFFSET ) * UPTIME_H_RATIO + UPTIME_H_ADDITION); } else{ //没有权限 showExitDialog(String.valueOf(pr_data_uptime_h)); showExitDialog(power+ "------power"); showExitDialog(x + "-----x"); showExitDialog(rtdate + "-----data"); tv_data_uptime_h.setText(String.valueOf(pr_data_uptime_h)); sb_data_uptime_h.setProgress(pr_data_uptime_h); } break; case R.id.sb_data_uptime_m: if(ispower() == 1) sendCommand(KEY_UPTIME_M, (seekBar.getProgress() + UPTIME_M_OFFSET ) * UPTIME_M_RATIO + UPTIME_M_ADDITION); else{ //没有权限 showExitDialog(String.valueOf(pr_data_uptime_m)); tv_data_uptime_m.setText(String.valueOf(pr_data_uptime_m)); sb_data_uptime_m.setProgress(pr_data_uptime_m); } break; default: break; } }