下面是我写的代码
public String urlCon(String input) {
try {
URL url = new URL(PATH + input);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
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);
}
return response.toString();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
我的想法是这个函数里面如果网很卡的话就会好很长时间,所以想把这个函数写在HANDLE方法里进行相应的操作 可行吗?要怎么实现呢?求方法。。
private Handler mhandler = new Handler() {//先建立一个handler
@Override
public void handleMessage(Message msg) {
if (msg.arg1 != -2) {
//执行你的方法
}
};
//在需要的地方
Message message = mhandler.obtainMessage();
message.arg1 = -2;
mhandler.sendMessage(message);
不懂再问,
public String urlCon(final String input){
Looper.prepare();
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.arg1 != 0123){
try {
URL url = new URL(PATH + input);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
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);
}
// System.out.println(response);
Log.i("返回的数据是 ", response.toString());
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
};
Looper.loop();
Message message = handler.obtainMessage();
message.arg1 = 0123;
handler.sendMessage(message);
// return urlCon(input);
}
这个返回值的地方想要返回 response 要怎么加啊
public class newclkas extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
System.out.println( urlCon("--------"));//你直接运行我这个代码看看
/*
handler里面本来就是主线程,你不能在主线程进行耗时操作。你要清楚,handler之所以用来更新主线程,是因为配合threed来实现的,,在run()中你可以调用handler进行一些ui更新,
*/
Message message = handler.obtainMessage();
message.arg1 = 0123;
}
});
thread.start();
}
public String urlCon( String input){
try {
URL url = new URL(input);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
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);
}
// System.out.println(response);
Log.i("返回的数据是 ", response.toString());
return response.toString();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.arg1 != 0123){
//你要知道,这个地方就是主线程。
//不能放一些耗时操作或者ui更新,
}
}
};
}