10-okHttp的同步与异步

我的理解如下:

同步: 实时的在等待返回结果;

异步:可以不是同步执行的,放入到执行队列中。

所以建议:如果需要根绝请求的结构做些判断应当用 同步,异步可能由于时间先后出现问题。

/*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;
		}
	}