mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-04 04:37:46 +08:00
🐛 #1220 修复公众号永久素材相关的部分okhttp实现有问题的接口
This commit is contained in:
parent
df1aa5c9dc
commit
779f1d08a9
@ -1,24 +1,24 @@
|
|||||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import me.chanjar.weixin.common.WxType;
|
import me.chanjar.weixin.common.WxType;
|
||||||
import me.chanjar.weixin.common.error.WxError;
|
import me.chanjar.weixin.common.error.WxError;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||||
import okhttp3.FormBody;
|
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.*;
|
||||||
import okhttp3.Request;
|
import org.slf4j.Logger;
|
||||||
import okhttp3.RequestBody;
|
import org.slf4j.LoggerFactory;
|
||||||
import okhttp3.Response;
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by ecoolper on 2017/5/5.
|
* .
|
||||||
|
*
|
||||||
|
* @author ecoolper
|
||||||
|
* @date 2017/5/5
|
||||||
*/
|
*/
|
||||||
public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestExecutor<OkHttpClient, OkHttpProxyInfo> {
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
@ -28,25 +28,25 @@ public class MaterialDeleteOkhttpRequestExecutor extends MaterialDeleteRequestEx
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType) throws WxErrorException, IOException {
|
public void execute(String uri, String data, ResponseHandler<Boolean> handler, WxType wxType)
|
||||||
|
throws WxErrorException, IOException {
|
||||||
handler.handle(this.execute(uri, data, wxType));
|
handler.handle(this.execute(uri, data, wxType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
public Boolean execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||||
logger.debug("MaterialDeleteOkhttpRequestExecutor is running");
|
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();
|
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();
|
String responseContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||||
if (error.getErrorCode() != 0) {
|
if (error.getErrorCode() != 0) {
|
||||||
throw new WxErrorException(error);
|
throw new WxErrorException(error);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -28,13 +28,11 @@ public class MaterialNewsInfoOkhttpRequestExecutor extends MaterialNewsInfoReque
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
public WxMpMaterialNews execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
|
||||||
|
|
||||||
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
|
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
|
||||||
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
|
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
|
||||||
Request request = new Request.Builder().url(uri).post(requestBody).build();
|
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();
|
String responseContent = response.body().string();
|
||||||
log.debug("响应原始数据:{}", responseContent);
|
log.debug("响应原始数据:{}", responseContent);
|
||||||
|
|
||||||
|
@ -38,8 +38,6 @@ public class MaterialUploadOkhttpRequestExecutor extends MaterialUploadRequestEx
|
|||||||
throw new FileNotFoundException();
|
throw new FileNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
//得到httpClient
|
|
||||||
|
|
||||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
OkHttpClient client = requestHttp.getRequestHttpClient();
|
||||||
|
|
||||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
|
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder()
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
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.WxType;
|
||||||
import me.chanjar.weixin.common.error.WxError;
|
import me.chanjar.weixin.common.error.WxError;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
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 me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -25,12 +27,11 @@ public class MaterialVideoInfoOkhttpRequestExecutor extends MaterialVideoInfoReq
|
|||||||
@Override
|
@Override
|
||||||
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
public WxMpMaterialVideoInfoResult execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||||
logger.debug("MaterialVideoInfoOkhttpRequestExecutor is running");
|
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();
|
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();
|
String responseContent = response.body().string();
|
||||||
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
WxError error = WxError.fromJson(responseContent, WxType.MP);
|
||||||
if (error.getErrorCode() != 0) {
|
if (error.getErrorCode() != 0) {
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package me.chanjar.weixin.mp.util.requestexecuter.material;
|
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.WxType;
|
||||||
import me.chanjar.weixin.common.error.WxError;
|
import me.chanjar.weixin.common.error.WxError;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||||
|
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
import okio.BufferedSink;
|
import okio.BufferedSink;
|
||||||
import okio.Okio;
|
import okio.Okio;
|
||||||
@ -27,7 +29,9 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material
|
|||||||
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
public InputStream execute(String uri, String materialId, WxType wxType) throws WxErrorException, IOException {
|
||||||
logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running");
|
logger.debug("MaterialVoiceAndImageDownloadOkhttpRequestExecutor is running");
|
||||||
OkHttpClient client = requestHttp.getRequestHttpClient();
|
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();
|
Request request = new Request.Builder().url(uri).get().post(requestBody).build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
String contentTypeHeader = response.header("Content-Type");
|
String contentTypeHeader = response.header("Content-Type");
|
||||||
@ -35,6 +39,7 @@ public class MaterialVoiceAndImageDownloadOkhttpRequestExecutor extends Material
|
|||||||
String responseContent = response.body().string();
|
String responseContent = response.body().string();
|
||||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||||
}
|
}
|
||||||
|
|
||||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(outputStream))) {
|
||||||
sink.writeAll(response.body().source());
|
sink.writeAll(response.body().source());
|
||||||
return new ByteArrayInputStream(outputStream.toByteArray());
|
return new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
|
@ -9,16 +9,16 @@ import me.chanjar.weixin.mp.api.WxMpService;
|
|||||||
import me.chanjar.weixin.mp.api.test.ApiTestModule;
|
import me.chanjar.weixin.mp.api.test.ApiTestModule;
|
||||||
import me.chanjar.weixin.mp.api.test.TestConstants;
|
import me.chanjar.weixin.mp.api.test.TestConstants;
|
||||||
import me.chanjar.weixin.mp.bean.material.*;
|
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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.*;
|
||||||
import static org.testng.Assert.assertNotNull;
|
|
||||||
import static org.testng.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 素材管理相关接口的测试
|
* 素材管理相关接口的测试
|
||||||
@ -27,7 +27,7 @@ import static org.testng.Assert.assertTrue;
|
|||||||
* @author codepiano
|
* @author codepiano
|
||||||
* @author Binary Wang
|
* @author Binary Wang
|
||||||
*/
|
*/
|
||||||
@Test(groups = "materialAPI")
|
@Test
|
||||||
@Guice(modules = ApiTestModule.class)
|
@Guice(modules = ApiTestModule.class)
|
||||||
public class WxMpMaterialServiceImplTest {
|
public class WxMpMaterialServiceImplTest {
|
||||||
@Inject
|
@Inject
|
||||||
@ -243,6 +243,15 @@ public class WxMpMaterialServiceImplTest {
|
|||||||
|
|
||||||
@Test(dependsOnMethods = {"testMaterialFileList"}, dataProvider = "allTestMaterial")
|
@Test(dependsOnMethods = {"testMaterialFileList"}, dataProvider = "allTestMaterial")
|
||||||
public void testDeleteMaterial(String mediaId) throws WxErrorException {
|
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);
|
boolean result = this.wxService.getMaterialService().materialDelete(mediaId);
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user