AsyncTask+HttpURLConnection网络获取数据
2018-01-17 09:07
399 查看
import android.os.AsyncTask;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by john on 2018/1/12.
*/
public class MyTask extends AsyncTask<String,Void,String>{
private Icallback icallback;
private String streamtostr;
private MyTask(){}
public MyTask(Icallback icallback) {
this.icallback = icallback;
}
@Override
protected String doInBackground(String... strings) {
String str="";
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if (urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
streamtostr = StreamtoStr.streamtostr(inputStream, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return streamtostr;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
icallback.updateUiByjson(s);
}
public interface Icallback{
void updateUiByjson(String string);
}
}
//StreamtoStr工具类
public static String streamtostr(InputStream stream, String s){
StringBuilder builder=new StringBuilder();
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(stream,s));
String string;
while((string=reader.readLine())!=null){
builder.append(string);
}
reader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by john on 2018/1/12.
*/
public class MyTask extends AsyncTask<String,Void,String>{
private Icallback icallback;
private String streamtostr;
private MyTask(){}
public MyTask(Icallback icallback) {
this.icallback = icallback;
}
@Override
protected String doInBackground(String... strings) {
String str="";
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if (urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
streamtostr = StreamtoStr.streamtostr(inputStream, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return streamtostr;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
icallback.updateUiByjson(s);
}
public interface Icallback{
void updateUiByjson(String string);
}
}
//StreamtoStr工具类
public static String streamtostr(InputStream stream, String s){
StringBuilder builder=new StringBuilder();
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(stream,s));
String string;
while((string=reader.readLine())!=null){
builder.append(string);
}
reader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
相关文章推荐
- AsyncTask 与 HttpURLConnection获取网络数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android通过HttpURLConnection链接到网络,并获取网络数据
- HttpURLConnection获取网络数据(实例:网路请求图片)
- HttpURLConnection获取网络数据工具类
- 使用 HttpURLConnection 获取不到网络数据
- HttpURLConnection和okHttp两种获取网络数据的实现方法
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- 通过api使用HttpURLConnection 获取返回天气数据。
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- HttpURLConnection连接网页和获取数据的使用实例
- HttpURLConnection、HttpClient请求网络数据、Xutils的HttpUtils 请求数据
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- Android 网络编程之HttpURLConnection(conn.getResponseCode() != 200,从url读取写入数据)
- 使用HttpURLConnection请求Gson网络解析数据
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- HttpURLConnection---网络请求数据
- HttpURLConnection模拟浏览器+网络数据抓取