【问题标题】:Connection string open but doesn't proceed连接字符串打开但不继续
【发布时间】:2017-06-09 03:14:59
【问题描述】:

我正在尝试将连接字符串转换为缓冲区输入流,然后再转换为字符串,以便我可以将其更改为 json 格式,但在调试程序后无法继续 in = new BufferedInputStream(conn.getInputStream()); 并直接进入return语句。请帮忙。 我关注了这篇文章: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;
}

【问题讨论】:

  • 我打赌你会得到一个例外...我赌 NOMTException ...
  • 如何解决?
  • 通过学习调试

标签: android json inputstream httpconnection


【解决方案1】:

字符串 str="示例"
StringBuffer sbf = new StringBuffer();

//将字符串缓冲区转换为字节 byte[] bytes = sbf.toString().getBytes();

InputStream inputStream = new ByteArrayInputStream(bytes);

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多