Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

In this page you can find the example usage for java.io InputStreamReader read.

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.i3xx.step.uno.impl.ScriptCacheImpl.java

/**
 * Reads a script from the cache/*from  w w  w .  j  a  v a2s . c om*/
 * 
 * @param index The index of the cached element
 * @return The String read
 * @throws IOException
 */
public String read(int index) throws IOException {
    ByteArrayInputStream bin = new ByteArrayInputStream(buffer[index]);
    InputStream in = compress ? new GZIPInputStream(bin) : bin;
    InputStreamReader r = new InputStreamReader(in);
    StringWriter w = new StringWriter();

    char[] cbuf = new char[1024];
    int c = 0;

    while ((c = r.read(cbuf)) > -1)
        w.write(cbuf, 0, c);

    r.close();
    w.close();

    return w.toString();
}

From source file:org.apache.roller.weblogger.business.themes.SharedThemeFromDir.java

/**
 * Load a single template file as a string, returns null if can't read file.
 *///ww w  . j av a2 s. c om
private String loadTemplateFile(File templateFile) {
    // Continue reading theme even if problem encountered with one file
    if (!templateFile.exists() && !templateFile.canRead()) {
        return null;
    }

    char[] chars = null;
    int length;
    try {
        chars = new char[(int) templateFile.length()];
        FileInputStream stream = new FileInputStream(templateFile);
        InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
        length = reader.read(chars);
    } catch (Exception noprob) {
        log.error("Exception reading theme [" + this.getName() + "] template file [" + templateFile + "]");
        if (log.isDebugEnabled())
            log.debug(noprob);
        return null;
    }

    return new String(chars, 0, length);
}

From source file:com.freedomotic.plugins.devices.tcw122bcm.Tcw122bcm.java

private void changeRelayStatus(String hostname, int hostport, String relayNumber, int control) {
    try {/* ww w  . j  a v a  2  s . co m*/
        String authString = USERNAME + ":" + PASSWORD;
        //System.out.println("auth string: " + authString);
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        //System.out.println("Base64 encoded auth string: " + authStringEnc); //FOR DEBUG
        //Create a URL for the desired  page 
        URL url = new URL("http://" + hostname + ":" + hostport + "/?" + relayNumber + "=" + control);
        LOG.info("Freedomotic sends the command " + url);
        URLConnection urlConnection = url.openConnection();
        // if required set the authentication
        if (HTTP_AUTHENTICATION.equalsIgnoreCase("true")) {
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        }
        InputStream is = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int numCharsRead;
        char[] charArray = new char[1024];
        StringBuffer sb = new StringBuffer();
        while ((numCharsRead = isr.read(charArray)) > 0) {
            sb.append(charArray, 0, numCharsRead);
        }
        String result = sb.toString();
    } catch (MalformedURLException e) {
        LOG.severe("Change relay status malformed URL " + e.toString());
    } catch (IOException e) {
        LOG.severe("Change relay status IOexception" + e.toString());
    }
}

From source file:com.android.wako.net.AsyncHttpPost.java

public boolean process() {
    try {//from   www  .  j  av  a2s  . co  m
        //            LogUtil.d(Tag, "---process---url="+url+"?"+getParames());
        request = new HttpPost(url);
        if (Constants.isGzip) {
            request.addHeader("Accept-Encoding", "gzip");
        } else {
            request.addHeader("Accept-Encoding", "default");
        }
        request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        //            LogUtil.d(AsyncHttpPost.class.getName(),
        //                    "AsyncHttpPost  request to url :" + url);

        if (parameter != null && parameter.size() > 0) {
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            for (RequestParameter p : parameter) {
                list.add(new BasicNameValuePair(p.getName(), p.getValue()));
            }

            ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        }
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(2);
            // ??
            byte[] header = new byte[2];
            int result = bis.read(header);
            // reset??
            bis.reset();
            // ?GZIP?
            int headerData = getShort(header);
            // Gzip ? ? 0x1f8b
            if (result != -1 && headerData == 0x1f8b) {
                LogUtil.d("HttpTask", " use GZIPInputStream  ");
                is = new GZIPInputStream(bis);
            } else {
                LogUtil.d("HttpTask", " not use GZIPInputStream");
                is = bis;
            }
            InputStreamReader reader = new InputStreamReader(is, "utf-8");
            char[] data = new char[100];
            int readSize;
            StringBuffer sb = new StringBuffer();
            while ((readSize = reader.read(data)) > 0) {
                sb.append(data, 0, readSize);
            }
            ret = sb.toString();
            bis.close();
            reader.close();
            return true;

        } else {
            mRetStatus = ResStatus.Error_Code;
            RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                    "??,??" + statusCode);
            //                ret = ErrorUtil.errorJson("ERROR.HTTP.001",
            //                        exception.getMessage());
        }

        LogUtil.d(AsyncHttpPost.class.getName(), "AsyncHttpPost  request to url :" + url + "  finished !");
    } catch (IllegalArgumentException e) {
        mRetStatus = ResStatus.Error_IllegalArgument;
        //            RequestException exception = new RequestException(
        //                    RequestException.IO_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.002", exception.getMessage());
        LogUtil.d(AsyncHttpGet.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (org.apache.http.conn.ConnectTimeoutException e) {
        mRetStatus = ResStatus.Error_Connect_Timeout;
        //            RequestException exception = new RequestException(
        //                    RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.003", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (java.net.SocketTimeoutException e) {
        mRetStatus = ResStatus.Error_Socket_Timeout;
        //            RequestException exception = new RequestException(
        //                    RequestException.SOCKET_TIMEOUT_EXCEPTION, Constants.ERROR_MESSAGE);
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.004", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        mRetStatus = ResStatus.Error_Unsupport_Encoding;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.UNSUPPORTED_ENCODEING_EXCEPTION, "?");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.005", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  UnsupportedEncodingException  " + e.getMessage());
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        mRetStatus = ResStatus.Error_HttpHostConnect;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.CONNECT_EXCEPTION, "");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.006", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  HttpHostConnectException  " + e.getMessage());
    } catch (ClientProtocolException e) {
        mRetStatus = ResStatus.Error_Client_Protocol;
        e.printStackTrace();
        //            RequestException exception = new RequestException(
        //                    RequestException.CLIENT_PROTOL_EXCEPTION, "??");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.007", exception.getMessage());
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        mRetStatus = ResStatus.Error_IOException;
        //            RequestException exception = new RequestException(
        //                    RequestException.IO_EXCEPTION, "??");
        //            ret = ErrorUtil.errorJson("ERROR.HTTP.008", exception.getMessage());
        e.printStackTrace();
        LogUtil.d(AsyncHttpPost.class.getName(),
                "AsyncHttpPost  request to url :" + url + "  IOException  " + e.getMessage());
    } catch (Exception e) {
        mRetStatus = ResStatus.Error_IOException;
        e.printStackTrace();
    }
    return false;
}

From source file:com.iloomo.net.AsyncHttpPost.java

@Override
public void run() {
    String ret = "";
    try {//from   w  w  w.j a  va  2s  .c o m
        for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) {
            try {
                request = new HttpPost(url);
                request.addHeader("Accept-Encoding", "default");

                if (parameter != null && parameter.size() > 0) {
                    List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
                    Set<String> keys = parameter.keySet();

                    for (String key : keys) {
                        list.add(new BasicNameValuePair(key, String.valueOf(parameter.get(key))));
                    }
                    ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
                }
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        // LogUtil.d("HttpTask", " use GZIPInputStream  ");
                        is = new GZIPInputStream(bis);
                    } else {
                        // LogUtil.d("HttpTask",
                        // " not use GZIPInputStream");
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                } else {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "??,??" + statusCode);
                    ret = ErrorUtil.errorJson("999", exception.getMessage());
                }

                break;
            } catch (Exception e) {
                if (i == HttpConstant.CONNECTION_COUNT - 1) {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "");
                    ret = ErrorUtil.errorJson("999", exception.getMessage());
                } else {
                    Log.d("connection url", "" + i);
                    continue;
                }
            }
        }
    } catch (IllegalArgumentException e) {
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                HttpConstant.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("999", exception.getMessage());
    } finally {
        if (!HttpConstant.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
    }
    super.run();
}

From source file:com.iloomo.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {//w w  w  .java  2  s.  c o m
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            Set<String> keys = parameter.keySet();
            for (String key : keys) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(EncodeUtils.encode(key));
                bulider.append("=");
                bulider.append(EncodeUtils.encode(String.valueOf(parameter.get(key))));
            }
            url += "?" + bulider.toString();
            Log.i("request", url);
        }
        for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) {
            try {
                request = new HttpGet(url);
                if (HttpConstant.isGzip) {
                    request.addHeader("Accept-Encoding", "gzip");
                } else {
                    request.addHeader("Accept-Encoding", "default");
                }
                // 
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                // ?
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                    // ByteArrayOutputStream content = new
                    // ByteArrayOutputStream();
                    // response.getEntity().writeTo(content);
                    // ret = new String(content.toByteArray()).trim();
                    // content.close();
                } else {
                    ret = ErrorUtil.errorJson("" + statusCode, "??,??" + statusCode);
                    continue;
                }

                break;
            } catch (Exception e) {
                if (i == HttpConstant.CONNECTION_COUNT - 1) {
                    ret = ErrorUtil.errorJson("999", "");
                } else {
                    Log.d("connection url", "" + i);
                    continue;
                }
            }
        }

    } catch (IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                HttpConstant.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("999", exception.getMessage());
    } finally {
        if (!HttpConstant.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
    }
    super.run();
}

From source file:com.changxiang.game.sdk.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {/* www  .  j  av a  2s .c o m*/
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            for (RequestParameter p : parameter) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(Util.encode(p.getName()));
                bulider.append("=");
                bulider.append(Util.encode(p.getValue()));
            }
            //            url += "?" + bulider.toString();
            url += "?" + bulider.toString() + "&sign="
                    + MD5Util.MD5(bulider.toString() + "1234" + CXGameConfig.SERVERKEY);

            System.out.println("===" + url);
        }
        LogUtil.d("AsyncHttpGet : ", url);
        for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) {
            try {
                request = new HttpGet(url);
                if (CXGameConfig.isGzip) {
                    request.addHeader("Accept-Encoding", "gzip");
                } else {
                    request.addHeader("Accept-Encoding", "default");
                }
                // 
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                // ?
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                } else {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "??,??" + statusCode);
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                    LogUtil.d("connection url", "" + i);
                    continue;
                }

                break;
            } catch (Exception e) {
                if (i == CXGameConfig.CONNECTION_COUNT - 1) {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "");
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                } else {
                    LogUtil.d("connection url", "" + i);
                    continue;
                }
            }
        }

    } catch (java.lang.IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                CXGameConfig.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("-2", exception.getMessage());
    } finally {
        if (!CXGameConfig.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}

From source file:com.android.wako.net.AsyncHttpGet.java

@Override
boolean process() {
    try {/*from  w ww .ja  v  a2s. c o  m*/
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            for (RequestParameter p : parameter) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }

                bulider.append(Utils.encode(p.getName()));
                bulider.append("=");
                bulider.append(Utils.encode(p.getValue()));
            }
            url += "?" + bulider.toString();
        }
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url);
        request = new HttpGet(url);
        /*
         * if(Constants.isGzip){ request.addHeader("Accept-Encoding",
         * "gzip"); }else{ request.addHeader("Accept-Encoding", "default");
         * }
         */
        // 
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
        // ?
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "statusCode=" + statusCode);
        if (statusCode == HttpStatus.SC_OK) {
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            bis.mark(2);
            // ??
            byte[] header = new byte[2];
            int result = bis.read(header);
            // reset??
            bis.reset();
            // ?GZIP?
            int headerData = getShort(header);
            // Gzip ? ? 0x1f8b
            if (result != -1 && headerData == 0x1f8b) {
                if (LogUtil.IS_LOG)
                    LogUtil.d(TAG, " use GZIPInputStream  ");
                is = new GZIPInputStream(bis);
            } else {
                if (LogUtil.IS_LOG)
                    LogUtil.d(TAG, " not use GZIPInputStream");
                is = bis;
            }
            InputStreamReader reader = new InputStreamReader(is, "utf-8");
            char[] data = new char[100];
            int readSize;
            StringBuffer sb = new StringBuffer();
            while ((readSize = reader.read(data)) > 0) {
                sb.append(data, 0, readSize);
            }
            ret = sb.toString();
            bis.close();
            reader.close();
            return true;

        } else {
            mRetStatus = ResStatus.Error_Code;
            RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                    "??,??" + statusCode);
        }

        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  finished !");

    } catch (IllegalArgumentException e) {
        mRetStatus = ResStatus.Error_IllegalArgument;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                Constants.ERROR_MESSAGE);
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (org.apache.http.conn.ConnectTimeoutException e) {
        mRetStatus = ResStatus.Error_Connect_Timeout;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (java.net.SocketTimeoutException e) {
        mRetStatus = ResStatus.Error_Socket_Timeout;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.SOCKET_TIMEOUT_EXCEPTION,
                Constants.ERROR_MESSAGE);
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  onFail  " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        mRetStatus = ResStatus.Error_Unsupport_Encoding;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.UNSUPPORTED_ENCODEING_EXCEPTION,
                "?");
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  UnsupportedEncodingException  "
                    + e.getMessage());
    } catch (org.apache.http.conn.HttpHostConnectException e) {
        mRetStatus = ResStatus.Error_HttpHostConnect;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.CONNECT_EXCEPTION, "");
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG,
                    "AsyncHttpGet  request to url :" + url + "  HttpHostConnectException  " + e.getMessage());
    } catch (ClientProtocolException e) {
        mRetStatus = ResStatus.Error_Client_Protocol;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.CLIENT_PROTOL_EXCEPTION,
                "??");
        e.printStackTrace();
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG,
                    "AsyncHttpGet  request to url :" + url + "  ClientProtocolException " + e.getMessage());
    } catch (IOException e) {
        mRetStatus = ResStatus.Error_IOException;
        e.printStackTrace();
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??");
        e.printStackTrace();
        if (LogUtil.IS_LOG)
            LogUtil.d(TAG, "AsyncHttpGet  request to url :" + url + "  IOException  " + e.getMessage());
    } catch (Exception e) {
        mRetStatus = ResStatus.Error_IOException;
        e.printStackTrace();
    }
    return false;
}

From source file:com.baofeng.game.sdk.net.AsyncHttpGet.java

@Override
public void run() {
    String ret = "";
    try {// w ww  .  ja  v  a2  s.  c om
        if (parameter != null && parameter.size() > 0) {
            StringBuilder bulider = new StringBuilder();
            for (RequestParameter p : parameter) {
                if (bulider.length() != 0) {
                    bulider.append("&");
                }
                bulider.append(Util.encode(p.getName()));
                bulider.append("=");
                bulider.append(Util.encode(p.getValue()));

            }
            //            url += "?" + bulider.toString();
            url += "?" + bulider.toString() + "&sign="
                    + MD5Util.MD5(bulider.toString() + "1234" + BFGameConfig.SERVERKEY);

            //            System.out.println("@@@"+bulider.toString());
        }
        LogUtil.d("AsyncHttpGet : ", url);
        for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) {
            try {
                request = new HttpGet(url);
                if (BFGameConfig.isGzip) {
                    request.addHeader("Accept-Encoding", "gzip");
                } else {
                    request.addHeader("Accept-Encoding", "default");
                }
                // 
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                // ?
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                } else {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "??,??" + statusCode);
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                    LogUtil.d("connection url", "" + i);
                    continue;
                }

                break;
            } catch (Exception e) {
                if (i == BFGameConfig.CONNECTION_COUNT - 1) {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "");
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                } else {
                    LogUtil.d("connection url", "" + i);
                    continue;
                }
            }
        }

    } catch (java.lang.IllegalArgumentException e) {

        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                BFGameConfig.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("-2", exception.getMessage());
    } finally {
        if (!BFGameConfig.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}

From source file:com.changxiang.game.sdk.net.AsyncHttpPost.java

@Override
public void run() {
    String ret = "";
    try {//  ww w  .j  a  va2s .  com
        for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) {
            try {
                request = new HttpPost(url);

                request.addHeader("Accept-Encoding", "default");

                if (parameter != null && parameter.size() > 0) {
                    List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
                    for (RequestParameter p : parameter) {
                        list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue())));
                        LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue());

                    }
                    StringBuffer sb = new StringBuffer();
                    for (int j = 0; j < list.size(); j++) {
                        sb.append(list.get(j));
                        if (!(j == list.size() - 1)) {
                            sb.append("&");
                        }
                    }

                    BasicNameValuePair bn = new BasicNameValuePair("sign",
                            MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY));

                    System.out.println(
                            "POST_SIGN:" + sb + "&sign=" + MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY));
                    list.add(bn);
                    ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
                }

                System.out.println("====" + url);
                httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout);
                httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
                HttpResponse response = httpClient.execute(request);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    // HttpManager.saveCookies(response);
                    InputStream is = response.getEntity().getContent();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    bis.mark(2);
                    // ??
                    byte[] header = new byte[2];
                    int result = bis.read(header);
                    // reset??
                    bis.reset();
                    // ?GZIP?
                    int headerData = getShort(header);
                    // Gzip ? ? 0x1f8b
                    if (result != -1 && headerData == 0x1f8b) {
                        is = new GZIPInputStream(bis);
                    } else {
                        is = bis;
                    }
                    InputStreamReader reader = new InputStreamReader(is, "utf-8");
                    char[] data = new char[100];
                    int readSize;
                    StringBuffer sb = new StringBuffer();
                    while ((readSize = reader.read(data)) > 0) {
                        sb.append(data, 0, readSize);
                    }
                    ret = sb.toString();
                    bis.close();
                    reader.close();

                } else {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "??,??" + statusCode);
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                }

                break;
            } catch (Exception e) {
                if (i == CXGameConfig.CONNECTION_COUNT - 1) {
                    RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                            "");
                    ret = ErrorUtil.errorJson("-1", exception.getMessage());
                } else {
                    LogUtil.d("connection url", "" + i);
                    continue;
                }
            }
        }
    } catch (java.lang.IllegalArgumentException e) {
        RequestException exception = new RequestException(RequestException.IO_EXCEPTION,
                CXGameConfig.ERROR_MESSAGE);
        ret = ErrorUtil.errorJson("-2", exception.getMessage());
    } finally {
        if (!CXGameConfig.IS_STOP_REQUEST) {
            Message msg = new Message();
            msg.obj = ret;
            msg.getData().putSerializable("callback", callBack);
            resultHandler.sendMessage(msg);
        }
        if (customLoadingDialog != null && customLoadingDialog.isShowing()) {
            customLoadingDialog.dismiss();
            customLoadingDialog = null;
        }
    }
    super.run();
}