简单配置:
//网络权限
<uses-permission android:name="android.permission.INTERNET"/>
compile 'com.zhy:okhttputils:2.6.2'
compile 'com.jakewharton:butterknife:7.0.1'//view注解
compile 'com.google.code.gson:gson:2.5'
compile 'com.github.bumptech.glide:glide:3.6.1'
<一>Application中okhttp的联网配置:
public class MyApplication extends Application {
public static Context mContext;
@Override
public void onCreate() {
super.onCreate();
//初始化上下文
mContext = this;
//初始化OkHttp
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
.addInterceptor(new LoggerInterceptor("TAG"))
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session){
return true;
}
})
.build();
OkHttpUtils.initClient(okHttpClient);
}
public static Context getContext() {
return mContext;
}
}
《二》请求路径的封装
public class ApiService {
//top路径
public static final String url= "xxx";
public static final String base_url= "xxx";
}
《三》接口回调的封装
public interface NetClient {
void error(String massage);
void sucess(String json);
}
《四》联网请求的二次封装
public class NetUtils
//get请求
public static void get(String url, final NetClient netClient){
OkHttpUtils.get()
.url(url)
.id(100)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
netClient.error(e.getMessage());
}
@Override
public void onResponse(String response, int id) {
netClient.sucess(response);
}
});
}
// 提交请求集合
//https://api.douban.com/v2/movie/top250?start=0&count=10
public static void post(String base_url, final NetClient netClient){
Map<String,String> params = new HashMap<>();
String start = "1";
String count = "10";
params.put("start",start);
params.put("count",count);
OkHttpUtils.get()
.url(base_url)
.params(params)
.id(100)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
netClient.error(e.getMessage());
}
@Override
public void onResponse(String response, int id) {
netClient.sucess(response);
}
});
}
}
《五》简单使用listview 展示一下数据
//若使用recycleview
//转载自:[http://blog.csdn.net/lmj623565791/article/details/45059587]
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@Bind(R.id.text)
TextView text;
List<MovieEntity.SubjectsBean> subjects;
@Bind(R.id.listview)
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//联网请求
getNetData();
}
private void getNetData() {
// NetUtils.get(ApiService.url, new NetClient() {
// @Override
// public void error(String massage) {
// Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
// }
//
// @Override
// public void sucess(String json) {
// if (json != null) {
// //解析数据
// parseToData(json);
// }
// }
// });
NetUtils.post(ApiService.base_url, new NetClient() {
@Override
public void error(String massage) {
Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
}
@Override
public void sucess(String json) {
if (json != null) {
//解析数据
parseToData(json);
}
}
});
}
private void parseToData(String json) {
MovieEntity movieEntity = new Gson().fromJson(json, MovieEntity.class);
text.setText(movieEntity.toString());
//集合数据
subjects = movieEntity.getSubjects();
//设置适配器
MyAdapter adapter = new MyAdapter(this,subjects);
listview.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
Context context;
List<MovieEntity.SubjectsBean> subjects;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<MovieEntity.SubjectsBean> subjects) {
this.context = context;
this.subjects = subjects;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return subjects.size();
}
@Override
public Object getItem(int position) {
return subjects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null){
//初始化viewholder
convertView = mInflater.inflate(R.layout.item,parent,false);
viewHolder = new ViewHolder();
//初始化view
viewHolder.textView = (TextView)
convertView.findViewById(R.id.tv_text);
//绑定tag
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(subjects.get(position).getTitle());
return convertView;
}
}
class ViewHolder{
TextView textView;
}
}