连接字符串已打开但未继续

时间:2017-01-24 10:49:10

标签: android json inputstream httpconnection

我正在尝试将连接字符串转换为缓冲区输入流,然后转换为字符串,以便我可以将其更改为json格式但是在调试程序后不会继续  in = new BufferedInputStream(conn.getInputStream()); 并直接返回声明。请帮忙。 我遵循了这篇文章:https://www.tutorialspoint.com/android/android_json_parser.htm

public String makeServiceCall(String input_url)
{
    String response=null;
    InputStream in=null;
    StringBuffer sb = new StringBuffer();
    try {

        URL url= new URL(input_url);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        in = new BufferedInputStream(conn.getInputStream());

        BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        String inputLine = "";
        while ((inputLine = br.readLine()) != null) {
            sb.append(inputLine);
        }
        response = sb.toString();

    }
    catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    }
    catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    }
    catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    }
    catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}

1 个答案:

答案 0 :(得分:0)

String str ="示例"
StringBuffer sbf = new StringBuffer();

//将String Buffer转换为字节  byte [] bytes = sbf.toString()。getBytes();

InputStream inputStream = new ByteArrayInputStream(bytes);