Blame view

src/com/ectrip/trips/net/BasePostProtocol.java 3.1 KB
07670a44   黄灿宏   畅游通核销app: 1.增加日志上...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  package com.ectrip.trips.net;
  
  import android.os.Handler;
  import android.os.Message;
  
  import com.ectrip.cyt.utils.LogUtil;
  
  import java.io.IOException;
  
  import okhttp3.MediaType;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.RequestBody;
  import okhttp3.Response;
  
  
  public class BasePostProtocol {
  
      private String url;
      private String mParamsStr;
      private Handler mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              switch (msg.what) {
                  case 100://成功
                      String result = (String) msg.obj;
                      callback.onSuccessed(result);
                      break;
                  case 101://失败
                      String error = (String) msg.obj;
                      callback.onFailure(error);
                      LogUtil.d("HTTP", error);
                      break;
              }
  
          }
      };
      INetCallBack callback;
  
      /**
       * post 请求的方法
       *
       * @param url      请求地址
       * @param paramStr 参数
       */
  
      public void loadMoreData(String url, String paramStr, final INetCallBack call) {
          LogUtil.d("HTTP", "【请求地址】" + url);
          LogUtil.d("HTTP", "【请求参数】" + paramStr);
          mParamsStr = paramStr;
          this.url = url;
          callback = call;
          callback.onPrepare();
          new Thread(new Runnable() {
  
              @Override
              public void run() {
                  loadDataFromNet();
              }
          }).start();
      }
  
      private void loadDataFromNet() {
          try {
              OkHttpClient okHttpClient = OkHttpFactory.getOkHttpClient();
              MediaType JSON = MediaType.parse("application/json; charset=utf-8");
              RequestBody formBody = RequestBody.create(JSON, mParamsStr);
              Request request = new Request.Builder().url(url).post(formBody).build();
              Response response;
              try {
                  response = okHttpClient.newCall(request).execute();
                  if (response.isSuccessful()) {
                      Message message = Message.obtain();
                      String result = response.body().string();
                      LogUtil.d("HTTP", result);
                      message.obj = result;
                      message.what = 100;
                      mHandler.sendMessage(message);
                  } else {
                      Message message = Message.obtain();
                      String error = response.body().string();
                      LogUtil.d("HTTP", error);
                      message.obj = error;
                      message.what = 101;
                      mHandler.sendMessage(message);
                  }
              } catch (IOException e) {
                  Message message = Message.obtain();
                  String error = LogUtil.getExMsg(e);
                  LogUtil.d("HTTP", error);
                  message.obj = error;
                  message.what = 101;
                  mHandler.sendMessage(message);
              }
          } catch (Exception e) {
              LogUtil.d("HTTP", LogUtil.getExMsg(e));
          }
  
      }
  
  }