Ejemplo n.º 1
0
 @Override
 public void run() {
   try {
     HttpRequest request = HttpRequest.put(this.getUrlString());
     this.setupSecurity(request);
     request.acceptCharset(CHARSET);
     request.headers(this.getHeaders());
     request.form(this.getParams());
     int code = request.code();
     String body = request.body(CHARSET);
     JSONObject response = new JSONObject();
     response.put("status", code);
     if (code >= 200 && code < 300) {
       response.put("data", body);
       this.getCallbackContext().success(response);
     } else {
       response.put("error", body);
       this.getCallbackContext().error(response);
     }
   } catch (JSONException e) {
     this.respondWithError("There was an error generating the response");
   } catch (HttpRequestException e) {
     if (e.getCause() instanceof UnknownHostException) {
       this.respondWithError(0, "The host could not be resolved");
     } else if (e.getCause() instanceof SSLHandshakeException) {
       this.respondWithError("SSL handshake failed");
     } else {
       this.respondWithError("There was an error with the request");
     }
   }
 }
Ejemplo n.º 2
0
    @Override
    // 1.data 통신
    public ArrayList<String> call() throws Exception {
      JSONResultString result = null;
      ArrayList<String> arrayList1 = new ArrayList<String>();
      try {

        HttpRequest request = post("http://192.168.0.5:8088/bitin/api/class/start-attd ");

        // reiquest 설정
        request.connectTimeout(2000).readTimeout(2000);
        // JSON  포맷으로 보내기  => POST 방식
        request.acceptCharset("UTF-8");
        request.acceptJson();
        request.accept(HttpRequest.CONTENT_TYPE_JSON);
        request.contentType("application/json", "UTF-8");

        // 데이터 세팅
        JSONObject params1 = new JSONObject();
        params1.put("classNo", classNo);
        params1.put("startTime", endTime);
        params1.put("timer", count_timer);

        Log.d("JoinData-->", params1.toString());
        request.send(params1.toString());

        // 3. 요청
        int responseCode = request.code();
        if (HttpURLConnection.HTTP_OK != responseCode) {
          Log.e("HTTP fail-->", "Http Response Fail:" + responseCode);
          return null;
        } else {
          Log.e("HTTPRequest-->", "정상");
        }

        // 4. JSON 파싱
        Reader reader = request.bufferedReader();
        // Log.d("Reader",reader);
        result = GSON.fromJson(reader, JSONResultString.class);
        reader.close();

        // 5. 사용하기
        Log.d("---> ResponseResult-->", result.getResult()); // "success"? or "fail"?
        Log.d("--->Data-->", result.getData().toString());

        return result.getData();

      } catch (Exception e3) {
        e3.printStackTrace();
      }
      return result.getData();
    }