Skip to content

Commit 779f1d0

Browse files
committed
🐛 #1220 修复公众号永久素材相关的部分okhttp实现有问题的接口
1 parent df1aa5c commit 779f1d0

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package me.chanjar.weixin.mp.util.requestexecuter.material;
22

3-
import java.io.IOException;
4-
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
7-
3+
import com.google.common.collect.ImmutableMap;
84
import me.chanjar.weixin.common.WxType;
95
import me.chanjar.weixin.common.error.WxError;
106
import me.chanjar.weixin.common.error.WxErrorException;
117
import me.chanjar.weixin.common.util.http.RequestHttp;
128
import me.chanjar.weixin.common.util.http.ResponseHandler;
139
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
14-
import okhttp3.FormBody;
15-
import okhttp3.OkHttpClient;
16-
import okhttp3.Request;
17-
import okhttp3.RequestBody;
18-
import okhttp3.Response;
10+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
11+
import okhttp3.*;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.io.IOException;
1916

2017
/**
21-
* Created by ecoolper on 2017/5/5.
18+
* .
19+
*
20+
* @author ecoolper
21+
* @date 2017/5/5
2222
*/
2323
public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
2424
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -28,25 +28,25 @@ public MaterialDeleteOkhttpRequestExecutor(RequestHttp requestHttp) {
2828
}
2929

3030
@Override
31-
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType) throws WxErrorException, IOException {
31+
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType)
32+
throws WxErrorException, IOException {
3233
handler.handle(this.execute(uri, data, wxType));
3334
}
3435

3536
@Override
3637
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
3738
logger.debug("MaterialDeleteOkhttpRequestExecutor is running");
38-
//得到httpClient
39-
OkHttpClient client = requestHttp.getRequestHttpClient();
4039

41-
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
40+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
41+
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
4242
Request request = new Request.Builder().url(uri).post(requestBody).build();
43-
Response response = client.newCall(request).execute();
43+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
4444
String responseContent = response.body().string();
4545
WxError error = WxError.fromJson(responseContent, WxType.MP);
4646
if (error.getErrorCode() != 0) {
4747
throw new WxErrorException(error);
48-
} else {
49-
return true;
5048
}
49+
50+
return true;
5151
}
5252
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialNewsInfoOkhttpRequestExecutor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ public MaterialNewsInfoOkhttpRequestExecutor(RequestHttp requestHttp) {
2828

2929
@Override
3030
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
31-
OkHttpClient client = requestHttp.getRequestHttpClient();
32-
3331
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
3432
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
3533
Request request = new Request.Builder().url(uri).post(requestBody).build();
3634

37-
Response response = client.newCall(request).execute();
35+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
3836
String responseContent = response.body().string();
3937
log.debug("响应原始数据:{}", responseContent);
4038

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialUploadOkhttpRequestExecutor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public WxMpMaterialUploadResult execute(String uri, WxMpMaterial material, WxTyp
3838
throw new FileNotFoundException();
3939
}
4040

41-
//得到httpClient
42-
4341
OkHttpClient client = requestHttp.getRequestHttpClient();
4442

4543
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVideoInfoOkhttpRequestExecutor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package me.chanjar.weixin.mp.util.requestexecuter.material;
22

3+
import com.google.common.collect.ImmutableMap;
34
import me.chanjar.weixin.common.WxType;
45
import me.chanjar.weixin.common.error.WxError;
56
import me.chanjar.weixin.common.error.WxErrorException;
67
import me.chanjar.weixin.common.util.http.RequestHttp;
78
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
9+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
810
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
911
import okhttp3.*;
1012
import org.slf4j.Logger;
@@ -25,12 +27,11 @@ public MaterialVideoInfoOkhttpRequestExecutor(RequestHttp requestHttp) {
2527
@Override
2628
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
2729
logger.debug("MaterialVideoInfoOkhttpRequestExecutor is running");
28-
//得到httpClient
29-
OkHttpClient client = requestHttp.getRequestHttpClient();
3030

31-
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
31+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
32+
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
3233
Request request = new Request.Builder().url(uri).post(requestBody).build();
33-
Response response = client.newCall(request).execute();
34+
Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
3435
String responseContent = response.body().string();
3536
WxError error = WxError.fromJson(responseContent, WxType.MP);
3637
if (error.getErrorCode() != 0) {

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/requestexecuter/material/MaterialVoiceAndImageDownloadOkhttpRequestExecutor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package me.chanjar.weixin.mp.util.requestexecuter.material;
22

3+
import com.google.common.collect.ImmutableMap;
34
import me.chanjar.weixin.common.WxType;
45
import me.chanjar.weixin.common.error.WxError;
56
import me.chanjar.weixin.common.error.WxErrorException;
67
import me.chanjar.weixin.common.util.http.RequestHttp;
78
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
9+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
810
import okhttp3.*;
911
import okio.BufferedSink;
1012
import okio.Okio;
@@ -27,14 +29,17 @@ public MaterialVoiceAndImageDownloadOkhttpRequestExecutor(RequestHttp requestHtt
2729
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
2830
logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running");
2931
OkHttpClient client = requestHttp.getRequestHttpClient();
30-
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
32+
33+
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
34+
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
3135
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
3236
Response response = client.newCall(request).execute();
3337
String contentTypeHeader = response.header("Content-Type");
3438
if ("text/plain".equals(contentTypeHeader)) {
3539
String responseContent = response.body().string();
3640
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
3741
}
42+
3843
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
3944
sink.writeAll(response.body().source());
4045
return new ByteArrayInputStream(outputStream.toByteArray());

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpMaterialServiceImplTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
import me.chanjar.weixin.mp.api.test.ApiTestModule;
1010
import me.chanjar.weixin.mp.api.test.TestConstants;
1111
import me.chanjar.weixin.mp.bean.material.*;
12-
import org.testng.annotations.*;
12+
import org.testng.annotations.DataProvider;
13+
import org.testng.annotations.Guice;
14+
import org.testng.annotations.Test;
1315

1416
import java.io.File;
1517
import java.io.IOException;
1618
import java.io.InputStream;
1719
import java.util.*;
1820

19-
import static org.testng.Assert.assertEquals;
20-
import static org.testng.Assert.assertNotNull;
21-
import static org.testng.Assert.assertTrue;
21+
import static org.testng.Assert.*;
2222

2323
/**
2424
* 素材管理相关接口的测试
@@ -27,7 +27,7 @@
2727
* @author codepiano
2828
* @author Binary Wang
2929
*/
30-
@Test(groups = "materialAPI")
30+
@Test
3131
@Guice(modules = ApiTestModule.class)
3232
public class WxMpMaterialServiceImplTest {
3333
@Inject
@@ -175,7 +175,7 @@ public void testDownloadMaterial(String mediaId) throws WxErrorException, IOExce
175175
}
176176
}
177177

178-
@Test(dependsOnMethods = {"testAddNews","testUploadMaterial"})
178+
@Test(dependsOnMethods = {"testAddNews", "testUploadMaterial"})
179179
public void testGetNewsInfo() throws WxErrorException {
180180
WxMpMaterialNews wxMpMaterialNewsSingle = this.wxService
181181
.getMaterialService().materialNewsInfo(this.singleNewsMediaId);
@@ -243,6 +243,15 @@ public void testMaterialFileList() throws WxErrorException {
243243

244244
@Test(dependsOnMethods = {"testMaterialFileList"}, dataProvider = "allTestMaterial")
245245
public void testDeleteMaterial(String mediaId) throws WxErrorException {
246+
this.delete(mediaId);
247+
}
248+
249+
@Test
250+
public void testDeleteMaterialDirectly() throws WxErrorException {
251+
this.delete("abc");
252+
}
253+
254+
public void delete(String mediaId) throws WxErrorException {
246255
boolean result = this.wxService.getMaterialService().materialDelete(mediaId);
247256
assertTrue(result);
248257
}

0 commit comments

Comments
 (0)