diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialDeleteOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialDeleteOkhttpRequestExecutor.java index 2f435ab460..15064413dd 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialDeleteOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialDeleteOkhttpRequestExecutor.java @@ -1,24 +1,24 @@ package me.chanjar.weixin.mp.util.requestexecuter.material; -import java.io.IOException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import com.google.common.collect.ImmutableMap; import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.ResponseHandler; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; -import okhttp3.FormBody; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; +import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; /** - * Created by ecoolper on 2017/5/5. + * . + * + * @author ecoolper + * @date 2017/5/5 */ public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestExecutor { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -28,25 +28,25 @@ public MaterialDeleteOkhttpRequestExecutor(RequestHttp requestHttp) { } @Override - public void execute(String uri, String data, ResponseHandler handler, WxType wxType) throws WxErrorException, IOException { + public void execute(String uri, String data, ResponseHandler handler, WxType wxType) + throws WxErrorException, IOException { handler.handle(this.execute(uri, data, wxType)); } @Override public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException { logger.debug("MaterialDeleteOkhttpRequestExecutor is running"); - //得到httpClient - OkHttpClient client = requestHttp.getRequestHttpClient(); - RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), + WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); Request request = new Request.Builder().url(uri).post(requestBody).build(); - Response response = client.newCall(request).execute(); + Response response = requestHttp.getRequestHttpClient().newCall(request).execute(); String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent, WxType.MP); if (error.getErrorCode() != 0) { throw new WxErrorException(error); - } else { - return true; } + + return true; } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialNewsInfoOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialNewsInfoOkhttpRequestExecutor.java index d8b07737a8..9a0b2c68b2 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialNewsInfoOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialNewsInfoOkhttpRequestExecutor.java @@ -28,13 +28,11 @@ public MaterialNewsInfoOkhttpRequestExecutor(RequestHttp requestHttp) { @Override public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException { - OkHttpClient client = requestHttp.getRequestHttpClient(); - RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); Request request = new Request.Builder().url(uri).post(requestBody).build(); - Response response = client.newCall(request).execute(); + Response response = requestHttp.getRequestHttpClient().newCall(request).execute(); String responseContent = response.body().string(); log.debug("响应原始数据:{}", responseContent); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialUploadOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialUploadOkhttpRequestExecutor.java index 5a651427b5..9f034def9c 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialUploadOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialUploadOkhttpRequestExecutor.java @@ -38,8 +38,6 @@ public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxTyp throw new FileNotFoundException(); } - //得到httpClient - OkHttpClient client = requestHttp.getRequestHttpClient(); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder() diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVideoInfoOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVideoInfoOkhttpRequestExecutor.java index edcc527756..b626fe930a 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVideoInfoOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVideoInfoOkhttpRequestExecutor.java @@ -1,10 +1,12 @@ package me.chanjar.weixin.mp.util.requestexecuter.material; +import com.google.common.collect.ImmutableMap; import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult; import okhttp3.*; import org.slf4j.Logger; @@ -25,12 +27,11 @@ public MaterialVideoInfoOkhttpRequestExecutor(RequestHttp requestHttp) { @Override public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException { logger.debug("MaterialVideoInfoOkhttpRequestExecutor is running"); - //得到httpClient - OkHttpClient client = requestHttp.getRequestHttpClient(); - RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), + WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); Request request = new Request.Builder().url(uri).post(requestBody).build(); - Response response = client.newCall(request).execute(); + Response response = requestHttp.getRequestHttpClient().newCall(request).execute(); String responseContent = response.body().string(); WxError error = WxError.fromJson(responseContent, WxType.MP); if (error.getErrorCode() != 0) { diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java index 5c0ddb6a83..39869b6cbb 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java @@ -1,10 +1,12 @@ package me.chanjar.weixin.mp.util.requestexecuter.material; +import com.google.common.collect.ImmutableMap; import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import okhttp3.*; import okio.BufferedSink; import okio.Okio; @@ -27,7 +29,9 @@ public MaterialVoiceAndImageDownloadOkhttpRequestExecutor(RequestHttp requestHtt public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException { logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running"); OkHttpClient client = requestHttp.getRequestHttpClient(); - RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); + + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), + WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); Request request = new Request.Builder().url(uri).get().post(requestBody).build(); Response response = client.newCall(request).execute(); String contentTypeHeader = response.header("Content-Type"); @@ -35,6 +39,7 @@ public InputStream execute(String uri, String materialId, WxType wxType) throws String responseContent = response.body().string(); throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP)); } + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) { sink.writeAll(response.body().source()); return new ByteArrayInputStream(outputStream.toByteArray()); diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java index afca1a2acf..024697cb70 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java @@ -9,16 +9,16 @@ import me.chanjar.weixin.mp.api.test.ApiTestModule; import me.chanjar.weixin.mp.api.test.TestConstants; import me.chanjar.weixin.mp.bean.material.*; -import org.testng.annotations.*; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.*; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; +import static org.testng.Assert.*; /** * 素材管理相关接口的测试 @@ -27,7 +27,7 @@ * @author codepiano * @author Binary Wang */ -@Test(groups = "materialAPI") +@Test @Guice(modules = ApiTestModule.class) public class WxMpMaterialServiceImplTest { @Inject @@ -175,7 +175,7 @@ public void testDownloadMaterial(String mediaId) throws WxErrorException, IOExce } } - @Test(dependsOnMethods = {"testAddNews","testUploadMaterial"}) + @Test(dependsOnMethods = {"testAddNews", "testUploadMaterial"}) public void testGetNewsInfo() throws WxErrorException { WxMpMaterialNews wxMpMaterialNewsSingle = this.wxService .getMaterialService().materialNewsInfo(this.singleNewsMediaId); @@ -243,6 +243,15 @@ public void testMaterialFileList() throws WxErrorException { @Test(dependsOnMethods = {"testMaterialFileList"}, dataProvider = "allTestMaterial") public void testDeleteMaterial(String mediaId) throws WxErrorException { + this.delete(mediaId); + } + + @Test + public void testDeleteMaterialDirectly() throws WxErrorException { + this.delete("abc"); + } + + public void delete(String mediaId) throws WxErrorException { boolean result = this.wxService.getMaterialService().materialDelete(mediaId); assertTrue(result); }