예제 #1
0
  private static HttpResponse transformResponse(Response response) {
    int code = response.code();
    String message = response.message();
    BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message);

    ResponseBody body = response.body();
    InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength());
    httpResponse.setEntity(entity);

    Headers headers = response.headers();
    for (int i = 0; i < headers.size(); i++) {
      String name = headers.name(i);
      String value = headers.value(i);
      httpResponse.addHeader(name, value);
      if ("Content-Type".equalsIgnoreCase(name)) {
        entity.setContentType(value);
      } else if ("Content-Encoding".equalsIgnoreCase(name)) {
        entity.setContentEncoding(value);
      }
    }

    return httpResponse;
  }
  private Boolean uploadFiles(String mFileName, String mFilePath) {
    webServices = new WebServices();
    jsonParserClass = new JsonParserClass();
    boolean isValid = false;
    byte[] bytes = null;
    String url = "";
    File file = null;
    try {
      url =
          webServices.FILEUPLOAD
              + "/"
              + mFileName; // "http://vsuppliervm.cloudapp.net:5132/FileAttachmentService.svc/FileUpload/";
      file = new File(mFilePath);
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      FileInputStream fis = new FileInputStream(file);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] buf = new byte[1024];
      try {
        for (int readNum; (readNum = fis.read(buf)) != -1; ) {
          bos.write(buf, 0, readNum); // no doubt here is 0
          System.out.println("read " + readNum + " bytes,");
        }
      } catch (IOException ex) {
      }
      bytes = bos.toByteArray();

    } catch (Exception e) {
      Log.d("Exception", "Exception");
      e.printStackTrace();
    }
    try {
      ByteArrayInputStream instream = new ByteArrayInputStream(bytes);
      HttpClient httpclient = new DefaultHttpClient();
      url += "/" + bytes.length;
      HttpPost httppost = new HttpPost(url);
      InputStreamEntity reqEntity = new InputStreamEntity(instream, bytes.length);
      httppost.setEntity(reqEntity);
      reqEntity.setContentType("binary/octet-stream");

      httppost.setHeader("Content-type", "application/octet-stream");
      reqEntity.setChunked(true); // Send in multiple parts if needed
      reqEntity.setContentEncoding("utf-8");
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();
      String result = EntityUtils.toString(entity);

      if (result == null || result.length() == 0) {
        isValid = false;
      } else {
        attachimageUrl = attachimageUrl + "," + jsonParserClass.parseAttachImageUrl(result);
        isValid = true;
      }

      int statusCode = response.getStatusLine().getStatusCode();
      Log.d("statusCode", "" + statusCode);
      if (statusCode != 200) {
        return false;
      }
      isValid = true;
    } catch (Exception e) {
      e.printStackTrace();
      Log.e("Exception in AsyncTask-doInBackgroung", e.toString());
    }
    return isValid;
  }