我在 android 改造中发送内容类型为 x-www-form-urlencoded 的帖子请求时遇到问题。不确定我在发送发布请求时犯的错误。当通过邮递员发送相同的请求时,我可以收到预期的正确响应。此外,我尝试通过 html 表单发送相同的响应并收到成功的响应
public class RequestManager {
private static Retrofit retrofit;
private static final String BASE_URL = "https://ipguat.apps.net.pk/Ecommerce/api/Transaction/";
private OkHttpClient okhttpClient;
static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
//MainActivity 从我发送发布请求的地方
private void sendPayment(String token) {
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(c);
Call<ResponseBody> call = service.sendPayment("102", "Arfeen Test", token, "00", "5", "03451234567", "arfeen@arfeen.me", "POSTMAN-TEST-ARF", "01", "Test Purchase", "www.facebook.com", "www.google.com", "TEST-01", formattedDate, "www.youtube.com");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
renderPage(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
}
);
}
// 提到 API 端点的接口
public interface TokenService {
@FormUrlEncoded
@POST("PostTransaction/")
Call<ResponseBody> sendPayment(@Field("MERCHANT_ID") String id,
@Field("MERCHANT_NAME") String merchantName,
@Field("TOKEN") String token,
@Field("PROCCODE") String proccode,
@Field("TXNAMT") String transaction,
@Field("CUSTOMER_MOBILE_NO") String mobile,
@Field("CUSTOMER_EMAIL_ADDRESS") String email,
@Field("SIGNATURE") String signature,
@Field("VERSION") String version,
@Field("TXNDESC") String productDescription,
@Field("SUCCESS_URL") String successUrl,
@Field("FAILURE_URL") String failureUrl,
@Field("BASKET_ID") String basketID,
@Field("ORDER_DATE") String orderDate,
@Field("CHECKOUT_URL") String checoutUrl);
}