package com.bwie.myokhttp.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* OKHttp的Get网络请求(同步)
*
* @param view
*/
public void doGetRequest_bak(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//实例化OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//通过Request的builder对象设置Get请求的请求地址返回Request对象
Request request = new Request.Builder().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build();
//通过newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call去进行网络请求并返回响应结果
Response response = call.execute();
//判断响应是否正确
if (response.isSuccessful()) {
//获取到响应体
ResponseBody responseBody = response.body();
//根据相应体获取到相应内容
String jsonStr = responseBody.string();
Log.i("t", "result=" + jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* OKHttp的Get网络请求(异步)
*
* @param view
*/
public void doGetRequest(View view) {
//实例化
OkHttpClient okHttpClient = new OkHttpClient();
//通过Request的builder对象设置Get请求的请求地址返回Request对象
//默认是get请求 ,get方法可以省略
Request request = new Request.Builder().get().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build();
//通过newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call去进行网络请求队列并通过接口回掉返回响应结果
//同步和异步的区别
call.enqueue(new Callback() {
//网络请求失败时提示
@Override
public void onFailure(Call call, IOException e) {
}
//网络请求成功后,服务器响应的结果
@Override
public void onResponse(Call call, Response response) throws IOException {
//判断响应结果是否正确 200
if (response.isSuccessful()) {
//InputStream inputStream=response.body().byteStream();
//byte[] bytes = response.body().bytes();
//根据响应体获取到响应内容
String jsonStr = response.body().string();
Log.i("t", "result=" + jsonStr);
}
}
});
}
/**
* OKHttp的Post网络请求(同步)
*
* @param view
*/
public void doPostRequest_bak(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//实例化OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//区别----------------------------------------
//通过FormBody的builder方法获取到buildr对象
//FormBody.Builder builder = new FormBody.Builder();
//通过builder设置表单请求数据
//builder.add("", "");
FormBody.Builder builder = new FormBody.Builder()
.add("mobile", "17611200379")
.add("password", "123456");
//需要设置编码 中文编码时用到
//builder.addEncoded("","");
//通过builder对象的build方法获取到FormBody对象
//FormBody extends RequestBody
//FormBody body = builder.build();
//----------------------------------------
//通过Request的builder对象设置post请求方式并设置请求地址并返回请求对象request
//第一种
//Request request = new Request.Builder().post(body).url("").build();
// 第二种
Request request = new Request.Builder().post(builder.build()).url("http://120.27.23.105/user/login").build();
//通过调用newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call请求网络获取到响应对象Response
Response response = call.execute();
//判断响应结果是否正确 200
if (response.isSuccessful()) {
String josnStr = response.body().string();
Log.i("t", "result=" + josnStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* OKHttp的Post网络请求(异步)
*
* @param view
*/
public void doPostRequest(View view) {
//实例化OkhttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//通过Formbody的builder对象设置请求的参数
FormBody.Builder builder = new FormBody.Builder()
.add("mobile", "17611200379")
.add("password", "123456");
Request request = new Request.Builder().post(builder.build()).url("http://120.27.23.105/user/login").build();
//通过newCall获取到Call对象
Call call = okHttpClient.newCall(request);
//通过Call对象调用enqueue进行异步请求并返回请求接口
call.enqueue(new Callback() {
//返回网络请求失败
@Override
public void onFailure(Call call, IOException e) {
}
//返回网络请求成功后服务器的响应结果
@Override
public void onResponse(Call call, Response response) throws IOException {
//返回相应是否成功
if (response.isSuccessful()) {
//通过响应体获取到响应内容
String jonsStr = response.body().string();
Log.i("t", "异步 result=" + jonsStr);
}
}
});
}
public void doFileUploadRequest(View view) {
}
public void doFileDownloadRequest(View view) {
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* OKHttp的Get网络请求(同步)
*
* @param view
*/
public void doGetRequest_bak(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//实例化OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//通过Request的builder对象设置Get请求的请求地址返回Request对象
Request request = new Request.Builder().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build();
//通过newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call去进行网络请求并返回响应结果
Response response = call.execute();
//判断响应是否正确
if (response.isSuccessful()) {
//获取到响应体
ResponseBody responseBody = response.body();
//根据相应体获取到相应内容
String jsonStr = responseBody.string();
Log.i("t", "result=" + jsonStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* OKHttp的Get网络请求(异步)
*
* @param view
*/
public void doGetRequest(View view) {
//实例化
OkHttpClient okHttpClient = new OkHttpClient();
//通过Request的builder对象设置Get请求的请求地址返回Request对象
//默认是get请求 ,get方法可以省略
Request request = new Request.Builder().get().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build();
//通过newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call去进行网络请求队列并通过接口回掉返回响应结果
//同步和异步的区别
call.enqueue(new Callback() {
//网络请求失败时提示
@Override
public void onFailure(Call call, IOException e) {
}
//网络请求成功后,服务器响应的结果
@Override
public void onResponse(Call call, Response response) throws IOException {
//判断响应结果是否正确 200
if (response.isSuccessful()) {
//InputStream inputStream=response.body().byteStream();
//byte[] bytes = response.body().bytes();
//根据响应体获取到响应内容
String jsonStr = response.body().string();
Log.i("t", "result=" + jsonStr);
}
}
});
}
/**
* OKHttp的Post网络请求(同步)
*
* @param view
*/
public void doPostRequest_bak(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//实例化OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//区别----------------------------------------
//通过FormBody的builder方法获取到buildr对象
//FormBody.Builder builder = new FormBody.Builder();
//通过builder设置表单请求数据
//builder.add("", "");
FormBody.Builder builder = new FormBody.Builder()
.add("mobile", "17611200379")
.add("password", "123456");
//需要设置编码 中文编码时用到
//builder.addEncoded("","");
//通过builder对象的build方法获取到FormBody对象
//FormBody extends RequestBody
//FormBody body = builder.build();
//----------------------------------------
//通过Request的builder对象设置post请求方式并设置请求地址并返回请求对象request
//第一种
//Request request = new Request.Builder().post(body).url("").build();
// 第二种
Request request = new Request.Builder().post(builder.build()).url("http://120.27.23.105/user/login").build();
//通过调用newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call请求网络获取到响应对象Response
Response response = call.execute();
//判断响应结果是否正确 200
if (response.isSuccessful()) {
String josnStr = response.body().string();
Log.i("t", "result=" + josnStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* OKHttp的Post网络请求(异步)
*
* @param view
*/
public void doPostRequest(View view) {
//实例化OkhttpClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//通过Formbody的builder对象设置请求的参数
FormBody.Builder builder = new FormBody.Builder()
.add("mobile", "17611200379")
.add("password", "123456");
Request request = new Request.Builder().post(builder.build()).url("http://120.27.23.105/user/login").build();
//通过newCall获取到Call对象
Call call = okHttpClient.newCall(request);
//通过Call对象调用enqueue进行异步请求并返回请求接口
call.enqueue(new Callback() {
//返回网络请求失败
@Override
public void onFailure(Call call, IOException e) {
}
//返回网络请求成功后服务器的响应结果
@Override
public void onResponse(Call call, Response response) throws IOException {
//返回相应是否成功
if (response.isSuccessful()) {
//通过响应体获取到响应内容
String jonsStr = response.body().string();
Log.i("t", "异步 result=" + jonsStr);
}
}
});
}
public void doFileUploadRequest(View view) {
}
public void doFileDownloadRequest(View view) {
}