我正在尝试使用HttpURLConnection在网上找到无线连接到我的Raspberry pi的草图程序..它正在捕获异常“需要一个整数或大小为1的字符串”而且我不知道错误在哪里。当用户点击UI上的按钮到Raspberry pi上运行的网络服务器时,程序基本上会发送一个字符或数字。这是send命令的一部分:
private void sendCommand(String command) {
final String cmd = command;
AsyncTask task = new AsyncTask() {
private String resp;
@Override
protected Object doInBackground(Object... params) {
try {
resp = Sender.getStringResponseFromGetRequest(
"http://192.168.1.111:8888/" + cmd);
} catch (IOException e) {
resp = e.getMessage();
}
return null;
}
};
}
@Override
protected void onPostExecute(Object result) {
if(!resp.equals("OK")){
Toast.makeText(ctx, resp, Toast.LENGTH_LONG).show();
}
super.onPostExecute(result);
}
};
task.execute();
}
}
这里是getStringResponse ..方法:
public static String getStringResponseFromGetRequest(String requestUrl)
throws IOException {
URL url1;
URLConnection urlConnection;
DataInputStream inStream;
url1 = new URL(requestUrl);
urlConnection = url1.openConnection();
((HttpURLConnection) urlConnection).setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.setUseCaches(false);
inStream = new DataInputStream(urlConnection.getInputStream());
return inStream.readLine();
}