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(); } }
OkHttp使用手册
最新推荐文章于 2024-06-13 16:44:52 发布