【问题标题】:Retrofit2 with MailGun in android not workingandroid中带有MailGun的Retrofit2无法正常工作
【发布时间】:2020-12-04 16:16:06
【问题描述】:

我在尝试在 Android 应用程序上使用 Retrofit2 通过 MailGun 发送电子邮件时遇到了实际问题。下面的代码没有给出任何错误,但也不起作用,而且我对 MailGun 或 Retrofit2 的工作方式知之甚少,甚至不知道从哪里开始对下面的代码进行故障排除,并且正在努力寻找在线帮助。

这是我的 MailHandler 类。

public class MailHandler {

private static final String TAG = "MailHandler";
public static final String ENDPOINT = "https://api.mailgun.net/v3/<domain redacted>/messages/";
public static final String ACCEPT_JSON_HEADER = "Accept: application/json";
public static final String BASIC = "Basic";

String apiKey = "api:key-<redacted>";
String fromText = "Boaty McBoatface<noreply@boatymcboatface.co.uk>";
private MailInterface api;

public interface MailInterface {
    @Headers({ACCEPT_JSON_HEADER})
    @FormUrlEncoded
    @POST("/messages")
    Call<Void> authUser (@Header("Authorization") String authorizationHeader,
                         @Field("from") String from,
                         @Field("to") String to,
                         @Field("subject") String subject,
                         @Field("text") String text);

}

public MailHandler() {
    Retrofit retrofit = getAuthAdapter();
    api = retrofit.create(MailInterface.class);
}

public Retrofit getAuthAdapter() {
    return new Retrofit.Builder().baseUrl(ENDPOINT).addConverterFactory(GsonConverterFactory.create(new Gson())).build();
}

public void newEmail(String email) {
    String authHeader = BASIC + " " + Base64.encodeToString(apiKey.getBytes(), Base64.NO_WRAP);
    String subj = "Test Subject";
    String body = "Test Email";
    api.authUser(authHeader, fromText, email, subj, body);
    }
}

【问题讨论】:

  • 从您发布的代码中,您创建了一个 Retrofit 调用对象,但从不执行它。您的 api.authUser 方法返回一个类型化的 Call 对象。在该对象上,您可以调用 enqueue 以异步执行网络请求。

标签: android retrofit2 mailgun


【解决方案1】:

响应很晚,但这里的问题是您正在访问 api 端点。 (以防它帮助别人)

https://api.mailgun.net/v3/&lt;domain redacted&gt;/messages/messages/ 不存在。您的基本 url 应该只是 https://api.mailgun.net/v3/&lt;domain redacted&gt;/,因为当您调用 authUser 方法时,改造会添加到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多