使用okhttp3简单执行网络请求
compile 'com.squareup.okhttp3:okhttp:3.6.0'
<uses-permission android:name="android.permission.INTERNET" />
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
* Created by Smile on 2017/5/3.
*/
public class HttpUtilWithOkHttp {
public static void sendHttpRequest(final String address, final Callback callback){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(address).build();
client.newCall(request).enqueue(callback);
}
}).start();
}
}
/**
* 执行网络请求
*/
private void requestNetwork(){
String url="http://app.zhuashihui.com/api/qunfa";
HttpUtilWithOkHttp httpUtilWithOkHttp=new HttpUtilWithOkHttp();
httpUtilWithOkHttp.sendHttpRequest(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseStr=response.body().string();
Bundle bundle=new Bundle();
bundle.putString("responseStr",responseStr);
Message message=new Message();
message.setData(bundle);
handler.sendMessage(message);
}
});
}