import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


/**
* Created by lenovo on 2017/12/12.
*/

public class MyNetTask extends AsyncTask<String,Void,String> {
// 2.申明接口对象
private IcallBack icallBack;

public MyNetTask(IcallBack icallBack) {

this.icallBack = icallBack;
}

//3.创建构造方法,将接口传进来
@Override
protected String doInBackground(String... stings) {
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(stings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str;
while ((str= reader.readLine())!=null){
builder.append(str);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

//Gson显示
//4.调用接口中的方法
icallBack.getJsonData(s);
}
//1,定义一个接口
public interface IcallBack{
void getJsonData(String jsonStr);
}
}