public class HttpUtils {
private static HttpUtils httpUtils;
private getjsonlisten getjsonlisten;
public static HttpUtils getInstance() {
if (httpUtils==null){
httpUtils=new HttpUtils();
}
return httpUtils;
}
public void getdata(String url){
Myasync myasync = new Myasync();
myasync.execute(url);
}
private class Myasync extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... strings) {
String path = strings[0];
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len=0;
while ((len=inputStream.read(bytes))!=-1){
bos.write(bytes,0,len);
}
inputStream.close();
bos.close();
String s = bos.toString();
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
getjsonlisten.datajson(s);
}
}
public interface getjsonlisten{
public void datajson(String json);
}
public void setjsonlisten(getjsonlisten getjsonlisten){
this.getjsonlisten=getjsonlisten;
}
}
HttpUtils
最新推荐文章于 2024-04-21 09:53:21 发布