我正在尝试将连接字符串转换为缓冲区输入流,然后转换为字符串,以便我可以将其更改为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;
}
字符串 str="example"
StringBuffer sbf = new StringBuffer();
将字符串缓冲区转换为字节 byte[] bytes = sbf.toString().getBytes();
InputStreaminputStream = new ByteArrayInputStream(bytes);