public class URLTools {
public URLTools() {
}
;
public URLTools get(final String path) {
new Thread() {
private HttpURLConnection httpURLConnection;
@Override
public void run() {
super.run();
try {
URL url = new URL(path);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(3000);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8");
String s = CharStreams.toString(inputStreamReader);
Message message = Message.obtain();
message.obj = s;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
return this;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String obj = (String) msg.obj;
data.getName(obj);
}
};
private HttpData data;
public void getData(HttpData data) {
this.data = data;
}
public interface HttpData {
void getName(String name);
}
}