无法从android中的HttpUrlConnection中的InputStreamReader获取响应

时间:2017-03-23 06:27:25

标签: android httpurlconnection bufferedreader inputstreamreader

执行5到6次后,我无法得到响应,并且消息也没有进入网关,应用程序被触发。在InputStreamReader中,我执行5次后无法得到任何响应。当我检查日志没有错误,但输入流没有打印。

XMLFunctions.java

  public static String getLocalCurtainControlResponse( String                              ipadress, String imvgid, String deviceid, String command, String value,  int slidedValue) {
                    String result = null;
                    BufferedReader  reader =null;

                    System.out.println("getLocalCurtainControlResponse : IN");
                    System.out.println("command : " + command);
                    System.out.println("value : " + value);


                    byte[] loginBytes = ("admin" + ":" + "admin").getBytes();

                    StringBuilder loginBuilder = new StringBuilder()
                            .append("Basic ")
                            .append(Base64.encodeToString(loginBytes, Base64.DEFAULT));

                    try {
                        URL url = new URL("http://" + ipadress + "/cgi-bin/WebInterface.cgi?ImvgId=" + imvgid + "%20&DeviceId=" + deviceid + "%20&Action=" + command + "%20&Value=" + value + "%20&Level=" + slidedValue + "%20&App=MID");
                        System.out.print("step0"+"http://" + ipadress + "/cgi-bin/WebInterface.cgi?ImvgId=" + imvgid + "%20&DeviceId=" + deviceid + "%20&Action=" + command + "%20&Value=" + value + "%20&Level=" + slidedValue + "%20&App=MID");
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //            connection.setRequestMethod("GET");
                        connection.addRequestProperty("Authorization", loginBuilder.toString());
            //            connection.connect();



                        InputStream in = connection.getInputStream();
                        System.out.print("step1"+connection);
                        StringBuilder sb = new StringBuilder();
                        System.out.print("step2"+sb);

                        reader = new BufferedReader(new InputStreamReader(in));
                        System.out.print("step3"+reader);
                        String line;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line);
                            sb.append("\n");
                        }
                        System.out.print("step4"+sb);
                        reader.close();
                        result = sb.toString();
                        System.out.print("step5"+result);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                        System.out.print("Hiiiiiiiiiiiiiiiiiiii"+e.getMessage());
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.print("Byeeeeeeeeee"+e.getMessage());
                    } finally {
                        if (null!=reader) {
                            try {
                                reader.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                System.out.print("Hellllloooooooo"+e.getMessage());
                            }
                        }
                    }

                    return result;
                } 

1 个答案:

答案 0 :(得分:0)

使用HttpUrlConnection找到解决方案Post and Get

    private Object doSendRequest(final int requestCode, String url, final Object params, final String sessionId, final String requestType, final Activity parentActivity) {
    URL myurl;
    Log.v("sessionId", sessionId + "");
    HttpsURLConnection httpsURLConnection = null;
    try {
        if (requestType.equalsIgnoreCase("GET")) {
            if (params.toString().length() >= 1) {
                if (url.contains("?")) {
                    url = (url + params.toString());
                } else {
                    url = (url + "?" + params.toString());
                }
            }
        }
        myurl = new URL(url);
        Log.v("RequestCode, URL", requestCode + " : " + url);
        httpsURLConnection = (HttpsURLConnection) myurl.openConnection();
        httpsURLConnection.setRequestMethod(requestType);
        httpsURLConnection.setUseCaches(true);
        if (isOnline(parentActivity)) {
            httpsURLConnection.addRequestProperty("Cache-Control", "max-age=0");
        } else {
            httpsURLConnection.setUseCaches(true);
            httpsURLConnection.addRequestProperty("Cache-Control", "max-stale=" + CACHE_STALE_TIME_OUT);
            httpsURLConnection.addRequestProperty("Cache-Control", "only-if-cached");
        }

        httpsURLConnection.setConnectTimeout(20 * ONE_SECOND);
        httpsURLConnection.setReadTimeout(20 * ONE_SECOND);
        httpsURLConnection.setInstanceFollowRedirects(true);
        httpsURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_APPLICATION_JSON);
        if (sessionId != null) {
            httpsURLConnection.setRequestProperty("sessionId", sessionId);
        }
        httpsURLConnection.setDoInput(true);
        if (!requestType.equalsIgnoreCase("GET")) {
            Log.v("RequestData", "" + params.toString());
            httpsURLConnection.setDoOutput(true);
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(httpsURLConnection.getOutputStream(), "UTF-8"));
                writer.write(params.toString());
                writer.flush();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(writer != null){
                    try {
                        writer.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

        }
        httpsURLConnection.connect();
    } catch (Exception e) {
        Log.v("Ex", "1");
        e.printStackTrace();
        handleException(requestCode, e, parentActivity);
        return null;
    }

    //---------------------READING THE RESPONSE---------------------------//

    BufferedReader bReader = null;
    try {
        final String newSessionId = httpsURLConnection.getHeaderField("sessionId");
        final int httpResponseCode = httpsURLConnection.getResponseCode();
        Log.v("Req, Response codes", requestCode+", "+httpResponseCode);

        if(httpResponseCode == 504 && !LegionUtils.isOnline(parentActivity)){
            parentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    hideProgressDialog();
                   showOfflineDialog(parentActivity);
                }
            });
            return null;
        }

        if (httpResponseCode == 401)  {
            parentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    hideProgressDialog();


                }
            });
            return null;
        } else if ((httpResponseCode == 400 || (httpResponseCode >= 500 && httpResponseCode <= 599)) && !(parentActivity instanceof CreateAccountActivity)) {
            parentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    hideProgressDialog();

                    networkCallback.onFailure(requestCode, null);
                }
            });
            return null;
        } else if ((httpResponseCode >= 500 && httpResponseCode <= 599) && (parentActivity instanceof CreateAccountActivity)) {
            parentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    networkCallback.onFailure(requestCode, null);
                }
            });
            return null;
        }

        InputStream inputStream;
        try {
            inputStream = httpsURLConnection.getInputStream();
        }catch (FileNotFoundException e){
            e.printStackTrace();
            inputStream = httpsURLConnection.getErrorStream();
        }

        if (inputStream != null) {
            bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String inputLine;
            final StringBuffer response = new StringBuffer();
            while ((inputLine = bReader.readLine()) != null) {
                response.append(inputLine);
            }
            bReader.close();

            parentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.v("RESPONSE " + requestCode, response.toString());
                    networkCallback.onSuccess(requestCode, response.toString(), newSessionId);
                }
            });
        }

    } catch (Exception e) {
        Log.v("Ex", "2");
        e.printStackTrace();
        handleException(requestCode, e, parentActivity);
    } finally {
        try {
            if (bReader != null) {
                bReader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

private void handleException(final int requestCode, Exception error, final Activity parentActivity) {
    final String errorResponse;
    if (error instanceof TimeoutException || error instanceof SocketTimeoutException || error instanceof UnknownHostException) {
        errorResponse = "Your request has been timed out. Please try again later.";
    } else if (error instanceof SSLException || error instanceof ConnectException) {
        errorResponse = null;
        parentActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideProgressDialog();
                showOfflineDialog(parentActivity);
            }
        });
    } /*else if (error instanceof FileNotFoundException) {
        errorResponse = "Invalid Request.\nPlease try again later.";
    } */else if (error instanceof IOException) {
        errorResponse = "Something went wrong. Please try again later.";
    } else if (error instanceof JSONException) {
        errorResponse = "Unexpected response.\nPlease try again later.";
    } else {
        errorResponse = null;
        parentActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideProgressDialog();
               showOfflineDialog(parentActivity);
            }
        });
    }
    Log.v("FAILURE ERR RESPONSE " + requestCode, errorResponse + "");
    parentActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            networkCallback.onFailure(requestCode, errorResponse);
        }
    });
}