如果你有一个java.io.InputStream对象,你应该如何处理该对象并产生一个String?
假设我有一个InputStream包含文本数据,并且我想将其转换为a String,所以例如我可以将其写入日志文件.
采取InputStream并将其转换为最简单的方法是String什么?
public String convertStreamToString(InputStream is) {
// ???
}
Run Code Online (Sandbox Code Playgroud) 在对如何执行此操作感到困惑之后(如此处和此处所示),我现在使用以下代码成功连接到我的服务器应用程序和适当的 RESTful 方法:
public void onFetchBtnClicked(View v){
if(v.getId() == R.id.FetchBtn){
Toast.makeText(getApplicationContext(), "You mashed the button, dude.", Toast.LENGTH_SHORT).show();
new CallAPI().execute("http://10.0.2.2:28642/api/Departments/GetCount?serialNum=4242");
}
}
public static class CallAPI extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String urlString=params[0]; // URL to call
String resultToDisplay = "";
InputStream in = null;
// HTTP Get
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception e ) {
System.out.println(e.getMessage()); …Run Code Online (Sandbox Code Playgroud)