jar包的下載地址
http://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp/3.2.0/okhttp-3.2.0.jar
gradle:
compile 'com.squareup.okhttp3:okhttp:3.2.0'
public class MainActivity extends Activity implements initHelper, View.OnClickListener {
private static final String TAG = "OK_HTTP";
private Button getButton;
private Button postButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(null);
initData();
}
@Override
public void initView(View view) {
getButton = (Button) this.findViewById(R.id.ok_get);
getButton.setOnClickListener(this);
postButton = (Button) this.findViewById(R.id.ok_post);
postButton.setOnClickListener(this);
}
@Override
public void initData() {
}
@Override
public void setData() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok_get:
getDataByGet("url");
break;
case R.id.ok_post:
break;
default:
break;
}
}
public void getDataByGet(final String path) {
new Thread() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(path).build();
try {
Response response = client.newCall(request).execute();
boolean success = response.isSuccessful();
if (success) {
ResponseBody body = response.body();
String content = body.string();
Log.i(TAG, "content:" + content);
} else {
int code = response.code();
Log.i(TAG, "code:" + code + ",response" + response.message());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
//提交字符串類型的參數(包括JSON)
public void getDataByPostString(final String path) {
new Thread() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json;charset=utf-8");
//第二個參數可以使json字符串或者String類型的
//create()方法可以傳遞String ,Json,byte[],File等
RequestBody params = RequestBody.create(JSON, "");
Request request = new Request.Builder().url(path).post(params).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
ResponseBody body = response.body();
String content = body.string();
} else {
int code = response.code();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
//提交鍵值對
public void getDataByPostMap(final String path) {
new Thread() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
//鍵值對 3.x之前是FormEncodingBuilder而非FormBody
RequestBody map = new FormBody.Builder()
.add("id", "admin")
.add("user", "admin")
.build();
Request request = new Request.Builder().url(path).post(map).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
ResponseBody body = response.body();
String content = body.string();
} else {
int code = response.code();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
//提交File
public void uploadFile(final String path, final File file) {
new Thread() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
MediaType type = MediaType.parse("application/text;charset=utf-8");
RequestBody fileBody = RequestBody.create(type, file);
Request request = new Request.Builder().url(path).post(fileBody).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
ResponseBody body = response.body();
String content = body.string();
} else {
int code = response.code();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void setOKHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
//設置緩存
File path = new File("");//緩存的路徑
int cacheSize = 10 * 1024 * 1024;//緩存的大小
Cache cache = new Cache(path, cacheSize);
builder.cache(cache);
//設置超時,第二個參數爲單位
builder.connectTimeout(5, TimeUnit.SECONDS);//連接超時
builder.readTimeout(5, TimeUnit.SECONDS);//讀超時
builder.writeTimeout(10, TimeUnit.SECONDS);//寫超時
OkHttpClient client = builder.build();
Request request = new Request.Builder().url("").build();
Call call = client.newCall(request);
//call.execute();
//取消請求
call.cancel();
}
}