Ejemplo n.º 1
0
 public static BaseResponse doObject(String url, String responseBody, Type t) {
   BaseResponse resp = null;
   if (url.contains("113.31.22.234:16383") || url.contains("113.31.20.67:8197")) {
     String a = "{\"data\":" + responseBody + "}";
     String b = a.replace("\"sendtime\":{},", "\"sendtime\":\"\",");
     Log.i(TAG + " Response: =====>", b);
     resp = JSONHelper.jsonToObj(b, t);
   } else if (url.contains("https://api.weixin.qq.com")) {
     String a = "{\"data\":" + responseBody + "}";
     resp = JSONHelper.jsonToObj(a, t);
   } else {
     resp = JSONHelper.jsonToObj(responseBody, t);
   }
   if (Config.DEBUG && !Config.GATEWAY_URL.contains("i.api.zhubajie.com")) {
     SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
     String time = format.format(new Date(System.currentTimeMillis()));
     String urf8body = JSONHelper.unicodeToUtf8(responseBody);
     String response = "";
     if (!urf8body.equals("")) {
       if (urf8body.startsWith("{")) {
         response = JsonFormatTool.formatJson(urf8body, " ").replace("\n", "<br>");
       } else {
         response = urf8body.replace("\n", "<br>");
       }
     }
     LogManager.getInstance()
         .addLog(
             "<p>"
                 + "==========URL=========="
                 + time
                 + "<br><font color=\"#ff0000\">"
                 + url
                 + "</p><p>[请求]</p><p> ==========RESPONSE==========<br><font color=\"#00ff00\">"
                 + response
                 + "</p>");
   }
   return resp;
 }
Ejemplo n.º 2
0
  public static synchronized BaseResponse doPostWithMoreFiles(
      String urlString, String data, Type t, Map<String, File> filesMap) {
    String end = System.getProperty("line.separator");
    String twoHyphens = "--";
    String boundary = "*****";

    BaseResponse resp = null;
    String urlStr = appendUrl(urlString);

    try {
      URL url = new URL(urlStr);
      HttpURLConnection con = (HttpURLConnection) url.openConnection();
      con.setDoInput(true);
      con.setDoOutput(true);
      con.setUseCaches(false);
      con.setRequestMethod("POST");
      con.setRequestProperty("Connection", "Keep-Alive");
      con.setRequestProperty("Charset", "UTF-8");
      con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
      con.connect();

      DataOutputStream ds = new DataOutputStream(con.getOutputStream());

      // 写入基本数据
      Map<Object, Object> objectMap = new HashMap<Object, Object>();
      JSONObject object = new JSONObject(data);
      Iterator<String> iterator = object.keys();
      String tempName = null;
      while (iterator.hasNext()) {
        tempName = iterator.next();
        objectMap.put(tempName, object.getString(tempName));
      }

      StringBuilder sb = new StringBuilder();
      for (Map.Entry<Object, Object> param : objectMap.entrySet()) {
        if (param.getKey().toString().equalsIgnoreCase("face")) {
          continue;
        }
        sb.append(twoHyphens + boundary + end);
        sb.append("Content-Disposition: form-data; name=\"" + param.getKey() + "\"" + end);
        sb.append(end);
        sb.append(param.getValue().toString() + end);
      }

      ds.write(sb.toString().getBytes("UTF-8")); // 发送表单字段数据
      Log.i(TAG + "=====>", sb.toString());
      ds.flush();

      // 写入文件

      Object[] keys = filesMap.keySet().toArray();

      for (Object key : keys) {

        File file = filesMap.get(key);

        StringBuilder split = new StringBuilder();
        split.append(twoHyphens + boundary + end);
        split.append(
            "Content-Disposition: form-data; name=\""
                + key.toString()
                + "\"; filename=\""
                + file.getName()
                + "\""
                + end);

        split.append(end);

        Log.i("....", split.toString());

        FileInputStream is = null;
        try {
          // 发送图片数据
          ds.writeBytes(split.toString());
          is = new FileInputStream(file);
          byte[] b = new byte[1024];
          int length = 0;
          while ((length = is.read(b)) != -1) {
            ds.write(b, 0, length);
          }
          ds.writeBytes(end);
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          if (is != null) {
            try {
              is.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }

      ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
      ds.flush();
      // ds.writeBytes(twoHyphens + boundary + end);
      // ds.writeBytes("Content-Disposition: form-data; " +
      // "name=\"face\";filename=\""
      // + file.getName() + "\"" + end);
      // ds.writeBytes(end);
      //
      // FileInputStream fStream = new FileInputStream(file);
      // int bufferSize = 1024;
      // byte[] buffer = new byte[bufferSize];
      // int length = -1;
      // while ((length = fStream.read(buffer)) != -1) {
      // ds.write(buffer, 0, length);
      // }
      // ds.writeBytes(end);
      // // ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
      // fStream.close();
      // ds.flush();
      ds.close();

      Log.d(TAG, "URL:" + urlStr);
      Log.d(TAG, "CODE:" + con.getResponseCode());

      if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {

        InputStream is = con.getInputStream();
        int ch;
        StringBuffer b = new StringBuffer();
        while ((ch = is.read()) != -1) {
          b.append((char) ch);
        }
        String responseBody = b.toString();

        Log.i(TAG + "=====>", responseBody);
        resp = JSONHelper.jsonToObj(responseBody, t);
        if (Config.DEBUG && Config.GATEWAY_URL.contains("mt.zhubajie.la")) {
          SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
          LogManager.getInstance()
              .addLog(
                  "<p>"
                      + "==========URL=========="
                      + format.format(new Date(System.currentTimeMillis()))
                      + "<br><font color=\"#ff0000\">"
                      + urlStr
                      + "</p><p>"
                      + "==========REQUEST==========<br><font color=\"#00ffff\">"
                      + sb.toString().replace("\n", "<br>")
                      + "</p><p> ==========RESPONSE==========<br><font color=\"#00ff00\">"
                      + JsonFormatTool.formatJson(JSONHelper.unicodeToUtf8(responseBody), " ")
                          .replace("\n", "<br>")
                      + "</p>");
        }
      } else {
        return new ErrorResponse("上传文件失败");
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return resp;
  }