我目前有一個連接建立到一些中間件,通過它我發送SQL查詢到數據庫。Android/Java - 將DataInputStream轉換爲字符串
我在將DataInputStream轉換爲可用格式(不管它是否是字符串)時遇到了一些麻煩。我看着another StackOverflow question,但是因爲使用
in.readLine();
是「過時」,這意味着什麼,這並沒有解決我的問題。我需要能夠讀取我的中間件的響應。
private class NetworkTask extends AsyncTask<String, Void, Integer>
{
protected Integer doInBackground(String... params)
{
Message message = networkHandler.obtainMessage();
String ip = "example ip";
int port = 1234;
try
{
InetAddress serverAddr = InetAddress.getByName(ip);
Log.d("EventBooker", "C: Connecting...");
Socket socket = new Socket(serverAddr, port);
DataInputStream in = new DataInputStream(socket.getInputStream());
try
{
Log.d("EventBooker", "C: Connected. Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(params[0]);
networkHandler.sendMessage(message);
Log.d("EventBooker", "C: Sent.");
}
catch (Exception e)
{
Log.e("EventBooker", "S: Error", e);
}
Log.d("EventBooker", "C: Reading...");
Log.d("EventBooker", "C: Response read, closing.");
socket.close();
Log.d("Eventbooker", "C: Closed.");
}
catch (Exception e)
{
Log.e("EventBooker", "C: Error", e);
}
return 1;
}
}
試試這個:http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string?rq=1 – Rotemmiz
@Rotemmiz - 我也嘗試過,也沒有運氣。 IOUtils不能在Android中使用嗎? Eclipse不喜歡使用IOUtils。 –