個人理解以下:java
同步: 實時的在等待返回結果;異步
異步:能夠不是同步執行的,放入到執行隊列中。ide
因此建議:若是須要根絕請求的結構作些判斷應當用 同步,異步可能因爲時間前後出現問題。post
/*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; } /*post同步請求: postDataWithParame */ public void ispower_tongbu(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient();//建立OkHttpClient對象 FormBody.Builder formBody = new FormBody.Builder();//建立表單請求體 formBody.add("username","ansen");//傳遞鍵值對參數 formBody.add("password","123"); //傳遞鍵值對參數 Request request = new Request.Builder() .url("http://172.25.1.234:8080/DormitoryHelper/user/login") .post(formBody.build())//傳遞請求體 .build(); Response response = null; response = client.newCall(request).execute();//獲得Response 對象 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 = 0; //無權限 } } } catch (Exception e) { e.printStackTrace(); } } }).start(); } /*************提示框*******************/ private void showExitDialog1(String num){ // String rt = String.valueOf(num); new AlertDialog.Builder(this) .setTitle("提示") .setMessage("您沒有修改權限" + num) .setPositiveButton("肯定", null) .show(); } private void showExitDialog(){ new AlertDialog.Builder(this) .setTitle("提示") .setMessage("您沒有修改權限") .setPositiveButton("肯定", null) .show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { int x = 0; switch (seekBar.getId()) { case R.id.sb_data_uptime_h: ispower_tongbu(); if(power == 1){ sendCommand(KEY_UPTIME_H, (seekBar.getProgress() + UPTIME_H_OFFSET ) * UPTIME_H_RATIO + UPTIME_H_ADDITION); } else{ //沒有權限 // showExitDialog1(String.valueOf(pr_data_uptime_h)); showExitDialog1(power+ "------power"); showExitDialog1(x + "-----x"); showExitDialog1(rtdate + "-----data"); showExitDialog(); 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: ispower_tongbu(); if(power == 1) { sendCommand(KEY_UPTIME_M, (seekBar.getProgress() + UPTIME_M_OFFSET) * UPTIME_M_RATIO + UPTIME_M_ADDITION); } else{ //沒有權限 // showExitDialog1(String.valueOf(pr_data_uptime_m)); showExitDialog(); tv_data_uptime_m.setText(String.valueOf(pr_data_uptime_m)); sb_data_uptime_m.setProgress(pr_data_uptime_m); } break; default: break; } }