Search in sources :

Example 1 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method handleResponse.

private String handleResponse(HttpURLConnection httpURLConnection) throws WeiboException {
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    int status = 0;
    try {
        status = httpURLConnection.getResponseCode();
    } catch (IOException e) {
        e.printStackTrace();
        httpURLConnection.disconnect();
        throw new WeiboException(errorStr, e);
    }
    if (status != HttpURLConnection.HTTP_OK) {
        return handleError(httpURLConnection);
    }
    return readResult(httpURLConnection);
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) WeiboException(org.qii.weiciyuan.support.error.WeiboException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 2 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method doUploadFile.

public boolean doUploadFile(String urlStr, Map<String, String> param, String path, String imageParamName, final FileUploaderHttpHelper.ProgressListener listener) throws WeiboException {
    String BOUNDARYSTR = getBoundry();
    File targetFile = new File(path);
    byte[] barry = null;
    int contentLength = 0;
    String sendStr = "";
    try {
        barry = ("--" + BOUNDARYSTR + "--\r\n").getBytes("UTF-8");
        sendStr = getBoundaryMessage(BOUNDARYSTR, param, imageParamName, new File(path).getName(), "image/png");
        contentLength = sendStr.getBytes("UTF-8").length + (int) targetFile.length() + 2 * barry.length;
    } catch (UnsupportedEncodingException e) {
    }
    int totalSent = 0;
    String lenstr = Integer.toString(contentLength);
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    FileInputStream fis = null;
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
        URL url = null;
        url = new URL(urlStr);
        Proxy proxy = getProxy();
        if (proxy != null) {
            urlConnection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            urlConnection = (HttpURLConnection) url.openConnection();
        }
        urlConnection.setConnectTimeout(UPLOAD_CONNECT_TIMEOUT);
        urlConnection.setReadTimeout(UPLOAD_READ_TIMEOUT);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Charset", "UTF-8");
        urlConnection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
        urlConnection.setRequestProperty("Content-Length", lenstr);
        ((HttpURLConnection) urlConnection).setFixedLengthStreamingMode(contentLength);
        urlConnection.connect();
        out = new BufferedOutputStream(urlConnection.getOutputStream());
        out.write(sendStr.getBytes("UTF-8"));
        totalSent += sendStr.getBytes("UTF-8").length;
        fis = new FileInputStream(targetFile);
        int bytesRead;
        int bytesAvailable;
        int bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024;
        bytesAvailable = fis.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        bytesRead = fis.read(buffer, 0, bufferSize);
        long transferred = 0;
        final Thread thread = Thread.currentThread();
        while (bytesRead > 0) {
            if (thread.isInterrupted()) {
                targetFile.delete();
                throw new InterruptedIOException();
            }
            out.write(buffer, 0, bufferSize);
            bytesAvailable = fis.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fis.read(buffer, 0, bufferSize);
            transferred += bytesRead;
            if (transferred % 50 == 0) {
                out.flush();
            }
            if (listener != null) {
                listener.transferred(transferred);
            }
        }
        out.write(barry);
        totalSent += barry.length;
        out.write(barry);
        totalSent += barry.length;
        out.flush();
        out.close();
        if (listener != null) {
            listener.waitServerResponse();
        }
        int status = urlConnection.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            String error = handleError(urlConnection);
            throw new WeiboException(error);
        }
        targetFile.delete();
    } catch (IOException e) {
        e.printStackTrace();
        throw new WeiboException(errorStr, e);
    } finally {
        Utility.closeSilently(fis);
        Utility.closeSilently(out);
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return true;
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) InterruptedIOException(java.io.InterruptedIOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) WeiboException(org.qii.weiciyuan.support.error.WeiboException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method readResult.

private String readResult(HttpURLConnection urlConnection) throws WeiboException {
    InputStream is = null;
    BufferedReader buffer = null;
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    try {
        is = urlConnection.getInputStream();
        String content_encode = urlConnection.getContentEncoding();
        if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
            is = new GZIPInputStream(is);
        }
        buffer = new BufferedReader(new InputStreamReader(is));
        StringBuilder strBuilder = new StringBuilder();
        String line;
        while ((line = buffer.readLine()) != null) {
            strBuilder.append(line);
        }
        //            AppLogger.d("result=" + strBuilder.toString());
        return strBuilder.toString();
    } catch (IOException e) {
        e.printStackTrace();
        throw new WeiboException(errorStr, e);
    } finally {
        Utility.closeSilently(is);
        Utility.closeSilently(buffer);
        urlConnection.disconnect();
    }
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) GZIPInputStream(java.util.zip.GZIPInputStream) WeiboException(org.qii.weiciyuan.support.error.WeiboException) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 4 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method doGet.

public String doGet(String urlStr, Map<String, String> param) throws WeiboException {
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    globalContext = null;
    InputStream is = null;
    try {
        StringBuilder urlBuilder = new StringBuilder(urlStr);
        urlBuilder.append("?").append(Utility.encodeUrl(param));
        URL url = new URL(urlBuilder.toString());
        AppLogger.d("get request" + url);
        Proxy proxy = getProxy();
        HttpURLConnection urlConnection;
        if (proxy != null) {
            urlConnection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            urlConnection = (HttpURLConnection) url.openConnection();
        }
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(false);
        urlConnection.setConnectTimeout(CONNECT_TIMEOUT);
        urlConnection.setReadTimeout(READ_TIMEOUT);
        urlConnection.setRequestProperty("Connection", "Keep-Alive");
        urlConnection.setRequestProperty("Charset", "UTF-8");
        urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        urlConnection.connect();
        return handleResponse(urlConnection);
    } catch (IOException e) {
        e.printStackTrace();
        throw new WeiboException(errorStr, e);
    }
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) WeiboException(org.qii.weiciyuan.support.error.WeiboException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with GlobalContext

use of org.qii.weiciyuan.support.utils.GlobalContext in project weiciyuan by qii.

the class JavaHttpUtility method readError.

private String readError(HttpURLConnection urlConnection) throws WeiboException {
    InputStream is = null;
    BufferedReader buffer = null;
    GlobalContext globalContext = GlobalContext.getInstance();
    String errorStr = globalContext.getString(R.string.timeout);
    try {
        is = urlConnection.getErrorStream();
        if (is == null) {
            errorStr = globalContext.getString(R.string.unknown_sina_network_error);
            throw new WeiboException(errorStr);
        }
        String content_encode = urlConnection.getContentEncoding();
        if (!TextUtils.isEmpty(content_encode) && content_encode.equals("gzip")) {
            is = new GZIPInputStream(is);
        }
        buffer = new BufferedReader(new InputStreamReader(is));
        StringBuilder strBuilder = new StringBuilder();
        String line;
        while ((line = buffer.readLine()) != null) {
            strBuilder.append(line);
        }
        AppLogger.d("error result=" + strBuilder.toString());
        return strBuilder.toString();
    } catch (IOException e) {
        e.printStackTrace();
        throw new WeiboException(errorStr, e);
    } finally {
        Utility.closeSilently(is);
        Utility.closeSilently(buffer);
        urlConnection.disconnect();
        globalContext = null;
    }
}
Also used : GlobalContext(org.qii.weiciyuan.support.utils.GlobalContext) GZIPInputStream(java.util.zip.GZIPInputStream) WeiboException(org.qii.weiciyuan.support.error.WeiboException) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 InterruptedIOException (java.io.InterruptedIOException)6 WeiboException (org.qii.weiciyuan.support.error.WeiboException)6 GlobalContext (org.qii.weiciyuan.support.utils.GlobalContext)6 FileInputStream (java.io.FileInputStream)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 Proxy (java.net.Proxy)3 URL (java.net.URL)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 HttpURLConnection (java.net.HttpURLConnection)2 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1