# OkHttpUtils **Repository Path**: cxbiao/OkHttpUtils ## Basic Information - **Project Name**: OkHttpUtils - **Description**: OkHttp的应用demo - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-06-07 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OkHttp的应用示例,基本都是鸿祥的代码,只是将gson改成了fastjson,这个效率更高些 ###异步Get访问 ``` OkHttpClientManager.getAsyn(SERVER_IP+"ListServlet?format=json", new OkHttpClientManager.ResultCallback>() { @Override public void onError(Request request, Exception e) { e.printStackTrace(); } @Override public void onResponse(List response) { Log.e("TAG", response.size() + ""); mTv.setText(response.get(1).title); } }); ``` ###文件上传 ``` public void uploadFile(View view) throws IOException { File file = new File(Environment.getExternalStorageDirectory(), "abc.mp3"); OkHttpClientManager.postAsyn(SERVER_IP+"UploadFileServlet",// new OkHttpClientManager.ResultCallback() { @Override public void onError(Request request, Exception e) { e.printStackTrace(); } @Override public void onResponse(String filePath) { Log.e("TAG", filePath); } },// file,// "formfile",// new OkHttpClientManager.Param[]{ new OkHttpClientManager.Param("filename", "发如雪"), new OkHttpClientManager.Param("filedes", "周杰伦的哥")} ); } ``` ###文件下载 ``` //不能下载中文 OkHttpClientManager.downloadAsyn(SERVER_IP+"files/qa.docx", Environment.getExternalStorageDirectory().getAbsolutePath(), new OkHttpClientManager.ResultCallback() { @Override public void onError(Request request, Exception e) { e.printStackTrace(); } @Override public void onResponse(String response) { //下载后的文件路径 Log.e("TAG", response); } }); ```