//按钮点击事件 <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get请求" android:onClick="getClick"/> //get请求 public void getClick(View view) { OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); Request request = new Request.Builder() .url("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12") .get() .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Log.e("---get","onResponse"+string); } }); }
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="post请求" android:onClick="postClick"/>
//post请求
public void postClick(View view) { OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); FormBody formBody = new FormBody.Builder() .add("pageSize","10") .add("currentPage","1") .build(); Request request = new Request.Builder() .url("http://43.143.146.165:7777/foods/postFoods") .post(formBody) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Log.e("post","onResponse"+string); } }); }
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下载" android:onClick="downClick"/>
//下载
public void downClick(View view) { OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); Request request = new Request.Builder() .url("https://cdn.jsdelivr.net/gh/LuckyZmj/imgbed@master/galleries/%E7%82%AB%E9%85%B7%E8%B7%91%E8%BD%A6/01.jpg") .get() .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { InputStream inputStream = response.body().byteStream(); byte[] buffer = new byte[1024]; int len=0; FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/car3.jpg"); while ((len=inputStream.read(buffer))!=-1){ fileOutputStream.write(buffer,0,len); } } }); }
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上传" android:onClick="upClick"/>
//上传
public void upLoadClick(View view) { //创建客户端 OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); //创建表单 MultipartBody multipartBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file","car2.jpg", RequestBody.create(MediaType.parse("media/jpg"),new File("/sdcard/car2.jpg"))) .build(); //设置请求 Request request = new Request.Builder() .url("http://10.161.6.10/car/") .post(multipartBody) .build(); //响应 okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { Log.e("----ssss","onResponse"+"上传成功"); } }); }