如题
先贴出我的代码
public String urlCon(final String input) {
Looper.prepare();
try {
URL url = new URL(PATH + input);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
connection.setReadTimeout(8000);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.flush();
connection.getResponseCode();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
Log.i("返回的数据是 ", response.toString());
Message message = new Message();
if (connection.getConnectTimeout() == 5000) {
message.what = 01234;
handler.sendEmptyMessage(message.what);
}
if (connection.getConnectTimeout() > 1000) {
message.what = 02234;
handler.sendEmptyMessage(message.what);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Looper.loop();
return null;
}
我在打印请求服务器后返回的数据后面又加了一个message方法 想设置我上面说的和其他的一些访问网络可能出现的情况的条件 连接失败,超时,读数据失败,超时,请求出错等等,请问这些方法的语句是怎么样的呢?求大神指导一下。。。万分感谢!!!!!