我正在尝试使用Mailgun和Jersey从Android应用发送电子邮件。问题是当我发布请求以获得响应时,应用程序崩溃并出现错误:A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/x-www-form-urlencoded, was not found
。我不确定为什么会这样。研究这个建议你在创建客户端时添加multipart类,但这也不起作用。请注意,在此电子邮件中,它只是纯文本,但我需要在应用程序中发送可能包含附件的另一封电子邮件 - 0个或更多图像。
java class:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client theClient = Client.create(cc);
theClient.addFilter(new HTTPBasicAuthFilter("api", "my_api_key"));
final WebResource theWebResource = theClient.resource("https://api.mailgun.net/v3/"
+ "mailgun_domain_key"
+ "/messages");
final FormDataMultiPart message = new FormDataMultiPart();
message.field("from", "My App <donotreply@dnr.com>");
message.field("to", "email_recs");
message.field("subject", "subj");
message.field("text", "body_text");
ClientResponse response = theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, message);
//app crashes after the above line is executed
build.gradle(module):
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.x.x"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions{
pickFirst 'META-INF/jersey-module-version'
pickFirst 'META-INF/services/com.sun.jersey.spi.inject.InjectableProvider'
pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyReader'
pickFirst 'META-INF/services/javax.ws.rs.ext.MessageBodyWriter'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
compile files('libs/jersey-client-1.19.jar')
compile files('libs/jersey-core-1.19.jar')
compile files('libs/jersey-multipart-1.19.jar')
compile files('libs/javax.ws.rs.jar')
compile files('libs/jai_imageio-1.1.jar')
}
答案 0 :(得分:0)
看看这个
theWebResource.type(MediaType.APPLICATION_FORM_URLENCODED)
您尝试发布一个旨在用作多部分请求的对象FormDataMultiPart
,但您要将Content-Type
设置为application/x-www-form-urlencoded
。
您只需将Content-Type
更改为multipart/form-data
[ 1 ] 或MediaType.MULTIPART_FORM_DATA
。
<子> 1。正如API documentation
中所述