@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,”正在加載中”,Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("xxxxxxxxxxxxx", s.toString());
}
@Override
protected String doInBackground(String... params) {
//獲取接口地址
String url = params[0];
//創建網絡請求方式
HttpClient client = new DefaultHttpClient();
//創建請求方式
HttpGet get = new HttpGet(url);
//通過網絡請求對象執行請求
try {
HttpResponse response = client.execute(get);
//判斷響應碼
if (response.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = inputStream.read(buffer)) != -1 ){
bos.write(buffer, 0, len);
}
inputStream.close();
bos.close();
String json = bos.toString();
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}