package com.bwei.httpclient;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
public class MainActivity extends AppCompatActivity {
private TextView tv_title;
//接口地址
private String path = "http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
initView();
//获取网络数据
getServerData();
}
private void initView() {
tv_title = (TextView) findViewById(R.id.tv_title);
}
private void getServerData() {
//获取异步任务对象
MyAsyncTask task = new MyAsyncTask();
//执行任务
task.execute(path);
}
private class MyAsyncTask extends AsyncTask<String,Integer,String>{
//onPreExecute方法运行在主线程 在doInBackground之前 做准备工作
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "正在加载中", Toast.LENGTH_SHORT).show();
}
//doInBackground方法运行在子线程 做耗时操作
@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;
}
//onProgressUpdate方法运行在主线程 在publishProgress方法执行之后才调用 更新进度
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
//onPostExecute方法运行在主线程 在doInBackground方法之后执行 更新UI
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("xxx",s.toString());
}
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
public class MainActivity extends AppCompatActivity {
private TextView tv_title;
//接口地址
private String path = "http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
initView();
//获取网络数据
getServerData();
}
private void initView() {
tv_title = (TextView) findViewById(R.id.tv_title);
}
private void getServerData() {
//获取异步任务对象
MyAsyncTask task = new MyAsyncTask();
//执行任务
task.execute(path);
}
private class MyAsyncTask extends AsyncTask<String,Integer,String>{
//onPreExecute方法运行在主线程 在doInBackground之前 做准备工作
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "正在加载中", Toast.LENGTH_SHORT).show();
}
//doInBackground方法运行在子线程 做耗时操作
@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;
}
//onProgressUpdate方法运行在主线程 在publishProgress方法执行之后才调用 更新进度
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
//onPostExecute方法运行在主线程 在doInBackground方法之后执行 更新UI
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("xxx",s.toString());
}
}
}
httpclinet请求网络依赖 compile 'com.loopj.android:android-async-http:1.4.9'