这是我的API详细信息,
POST /dashboard/sales/tasks/entry/new HTTP/1.1
Host: xxxxxxxx.com
Connection: keep-alive
Content-Length: 617
RequestVerificationToken: kk54G4tCukmg1yernJnHBCTJ3ma9qIIws8sXJAQE5gdeDs-Gzr_5BRZ3YCGUtf8Gb9V-sktGlBrqIPjMiPZoxw4e3ICyS2vPR0el4bbnJYTuitTzFgeSvP8VF4fOpVBzj3sZ5J5zinpLYs8EiFMSxw2
Origin: http://xxxxxxxx.com
Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJGcmFwaWQiLCJpYXQiOiI2MzY3OTY5NjYyMDU3OTg1MDMiLCJleHAiOiI2MzY3OTc4MzAyMDU3OTg1MDMiLCJzdWIiOiJlcnBfd2FzbHRlY19vcmciLCJsb2dpbmlkIjoiMTIxMzYifQ.FAemEhwzQrMonpExC9arvO1IPitbux0O2RdE3u8Uqkjaa7zBP3hYUGjD1DaXzm9zVWuYInw_KpLtVrBcj6Q6Rw
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36xxxxxxxx.com
Referer: http://xxxxxxxx.com/dashboard/sales/tasks/entry/new
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,ar;q=0.8
Cookie: __RequestVerificationToken=qy3A36OVllT36gL8UD1pDjrGw9RjntkpvXuIyNLZ2f8oK_Lvgprp11osb-oBpdsYPNRW5UixruCpJUmGRogSxq6caLbtcFoG58TY8Q8kM4o1; culture=en-US; access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJGcmFwaWQiLCJpYXQiOiI2MzY3OTY5NjYyMDU3OTg1MDMiLCJleHAiOiI2MzY3OTc4MzAyMDU3OTg1MDMiLCJzdWIiOiJlcnBfd2FzbHRlY19vcmciLCJsb2dpbmlkIjoiMTIxMzYifQ.FAemEhwzQrMonpExC9arvO1IPitbux0O2RdE3u8Uqkjaa7zBP3hYUGjD1DaXzm9zVWuYInw_KpLtVrBcj6Q6Rw
这是我正在传递的请求格式,
{
"CostCenterId": "1",
"ShipperId": "1",
"PriceTypeId": "1",
"StoreId": "1",
"CounterId": "1",
"BookDate": "2018-12-02T14:48:17.709+03:00",
"ValueDate": "2018-12-02T14:48:17.709+03:00",
"Tender": "5000.0",
"Change": "3845.0",
"CustomerId": "1",
"CustomerName": "DEFAULT",
"DiscountRate": "1",
"Discount": "0",
"Details": [
{
"StoreId": "1",
"ItemId": "228",
"Quantity": "1",
"Price": "1155.0",
"UnitId": "1",
"DiscountRate": "0",
"Discount": "0"
}
]
}
当我在邮递员上使用相同的请求Json和用户令牌(eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJGcmFwaWQiLCJpYXQiOiI2MzY3OTY5NjYyMDU3OTg1MDMiLCJleHAiOiI2MzY3OTc4MzAyMDU3OTg1MDMiLCJzdWIiOiJlcnBfd2FzbHRlY19vcmciLCJsb2dpbmlkIjoiMTIxMzYifQ.FAemEhwzQrMonpExC9arvO1IPitbux0O2RdE3u8Uqkjaa7zBP3hYUGjD1DaXzm9zVWuYInw_KpLtVrBcj6Q6Rw
)时,我会收到成功响应。但是在应用程序上,我收到错误HTML(此HTML是登录表单)。这是我正在使用的Android代码,
ApiClient.java
public class ApiClient {
public static HttpLoggingInterceptor getInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
static OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(40000, TimeUnit.SECONDS)
.writeTimeout(40000, TimeUnit.SECONDS)
.readTimeout(40000, TimeUnit.SECONDS)
.addInterceptor(getInterceptor())
.build();
static ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
static Gson gson = new GsonBuilder()
.setLenient()
.create();
static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppController.getInstance().getBaseUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
private static Retrofit getClient() {
gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder()
.baseUrl(AppController.getInstance().getBaseUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
}
public static Call<Object> performCheckout(String token, JSONObject jsonObject) {
token = "Bearer " + token;
Call<Object> call = apiService.performCheckout(token, jsonObject);
return call;
}
}
ApiInterface.Java
public interface ApiInterface {
@Headers({
"Content-Type: application/json; charset=UTF-8"
})
@POST("dashboard/sales/tasks/entry/new")
Call<Object> performCheckout(@Header("Authorization") String token,@Body JSONObject jsonObject);
}
//生成JSONObject的代码
public static JSONObject getCheckoutRequest(float tenderAmt, float changeAmt, int storeId, int counterId, Customer customer, int discountType, String discount) {
JSONObject jsonObject = new JSONObject();
try {
String checkoutTime = Helper.getCurrentDateTime();
JSONArray productsArray = getSaleProductsArrayRequest(storeId);
jsonObject.put("CostCenterId", "1");
jsonObject.put("ShipperId", "1");
jsonObject.put("PriceTypeId", "1");
jsonObject.put("StoreId", String.valueOf(storeId));
jsonObject.put("CounterId", String.valueOf(counterId));
jsonObject.put("BookDate", checkoutTime);
jsonObject.put("ValueDate", checkoutTime);
jsonObject.put("Tender", String.valueOf(tenderAmt));
jsonObject.put("Change", String.valueOf(changeAmt));
jsonObject.put("CustomerId", String.valueOf(customer.getCustomerId()));
jsonObject.put("CustomerName", customer.getCustomerName());
jsonObject.put("DiscountRate", String.valueOf(discountType));
jsonObject.put("Discount", String.valueOf(discount));
jsonObject.put("Details", productsArray);
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONArray getSaleProductsArrayRequest(int storeId) {
JSONArray attributesArray = new JSONArray();
ArrayList<CartItem> cartItems = AppController.getInstance().getCart();
try {
for (CartItem item : cartItems) {
Product product = AppController.getInstance().getProductDetails(item.getItemId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("StoreId", String.valueOf(storeId));
jsonObject.put("ItemId", String.valueOf(item.getItemId()));
jsonObject.put("Quantity", String.valueOf(item.getQuantity()));
float price = product.getSellingPrice();
if (!product.isSellingPriceIncludesTax()) {
price = (float) (product.getSellingPrice() * 1.05);
}
jsonObject.put("Price", String.valueOf(price));
jsonObject.put("UnitId", "1");
jsonObject.put("DiscountRate", "0");
jsonObject.put("Discount", "0");
attributesArray.put(jsonObject);
}
} catch (Exception e) {
e.printStackTrace();
}
return attributesArray;
}
API调用
final String token = AppSettings.getInstance(getActivity()).getStringValue(PrefKeys.UserToken);
JSONObject request = RequestModel.getCheckoutRequest(tenderCash, changeAmt, storeId, counter.getCounterId(), customer, discountType, discount);
Log.d(TAG, "Token: " + token);
Log.d(TAG, "Request: " + request.toString());
if (token != null && request != null && customer != null && counter != null) {
progressLayout.setVisibility(View.VISIBLE);
ApiClient.performCheckout(token, request).enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
progressLayout.setVisibility(View.GONE);
if (response != null && response.isSuccessful()) {
Log.e(TAG, "Checkout Response : " + response.body().toString());
String invoiceNo = response.body().toString();
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
Log.e(TAG, "Checkout Error: " + t.getMessage());
progressLayout.setVisibility(View.GONE);
Helper.showCommonOkDialog(getActivity(), getString(R.string.Server_Error));
}
});
}