1

长期以来我一直在研究这个问题,但我没有成功。我仍在努力解决这个问题。

我在我的应用程序中使用了 Retrofit Api 2.6.2 版本。

如果我用 Postman 命中 api,它会成功运行。

1. 我从 google api 获取地址。下面是我得到的结果。

地址[addressLines=[0:"Av. Pueyrredón 1443, C1118AAE CABA, Argentina"],feature=1443,admin=Buenos Aires,sub-admin=Comuna 2,locality=null,thoroughfare=Avenida Pueyrredón,postalCode=C1118,countryCode =AR,countryName=Argentina,hasLatitude=true,latitude=-34.594003199999996,hasLongitude=true,longitude=-58.4018958,phone=null,url=null,extras=null]

  1. 我正在使用以下请求在服务器上更新此地址。

网址: http ://ec2-52-43-39-199.us-west-2.compute.amazonaws.com:8080/readyBApp/rest/profile/saveOrUpdateAddress?addressId=0&userId=1216&address1=Avenida%20Pueyrred%C3% B3n&address2=1443&state=Buenos%20Aires&zip=C1118&country=Argentina&propertyName=kk&isPrimaryAddress=true&isDefaultAddress=true&longitude=-58.401897&latitude=-34.594&userRole=po&ccStripeToken=card_1FKhmXKp2WkwUzXPqrGx1vV9&isByWizard=

使用 okhttp 记录请求改造:

D/OkHttp: --> POST http://xxx.compute.amazonaws.com:8080/readyBApp/rest/profile/saveOrUpdateAddress
2019-11-21 11:03:14.867 19512-19981/com.app.test D/OkHttp: Content-Type: application/x-www-form-urlencoded
2019-11-21 11:03:14.867 19512-19981/com.app.test D/OkHttp: Content-Length: 288
2019-11-21 11:03:14.867 19512-19981/com.app.test D/OkHttp: addressId=0&userId=1216&address1=Avenida%20Pueyrred%C3%B3n&address2=1443&state=Buenos%20Aires&zip=C1118&country=Argentina&propertyName=kk&isPrimaryAddress=true&isDefaultAddress=true&longitude=-58.401897&latitude=-34.594&userRole=po&ccStripeToken=card_1FKhmXKp2WkwUzXPqrGx1vV9&isByWizard=0
2019-11-21 11:03:14.867 19512-19981/com.app.test D/OkHttp: --> END POST (288-byte body)

使用 okhttp 进行响应改造的日志:

D/OkHttp: <-- 200 OK http://xxxxxxx.compute.amazonaws.com:8080/readyBApp/rest/profile/saveOrUpdateAddress (355ms)
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Expires: 0
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Cache-Control: no-cache, no-store, max-age=0, must-revalidate
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: X-Powered-By: Undertow/1
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Server: WildFly/10
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: X-XSS-Protection: 1; mode=block
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Pragma: no-cache
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: X-Frame-Options: DENY
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Date: Thu, 21 Nov 2019 05:29:57 GMT
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Connection: keep-alive
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: X-Content-Type-Options: nosniff
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: Content-Type: application/json;charset=UTF-8
2019-11-21 11:03:15.223 19512-19981/com.app.test D/OkHttp: {"success":1,"message":"The address was saved!","data":2456}

问题:地址 1 转换错误字符:

{
  "addressId": 2851,
  "isPrimaryAddress": true,
  "isDefaultAddress": true,
  "propertyName": "kk",
  "address1": "Avenida Pueyrredón",
  "address2": "1443",
  "city": null,
  "state": "Buenos Aires",
  "zip": "C1118",
  "country": "Argentina",
  "longitude": -58.4019,
  "latitude": -34.594,
  "active": true,
  "userId": null,
  "address3": null,
  "cardId": 928,
  "defaultCard": false,
  "isCardActive": true,
  "userRole": "po"
}

改造 API:

@FormUrlEncoded
@POST("readyBApp/rest/profile/saveOrUpdateAddress")
Observable<ResponseBody> saveOrUpdateAddress(@Field("addressId") long addressId, @Field("userId") long userId, @Field("address1") String address1, @Field("address2") String address2, @Field("city") String city, @Field("state") String state, @Field("zip") String zip, @Field("country") String country, @Field("propertyName") String propertyName, @Field("isPrimaryAddress") boolean isPrimary, @Field("isDefaultAddress") boolean isDefault, @Field("longitude") float longitude, @Field("latitude") float latitude, @Field("userRole") String userRole, @Field("ccStripeToken") String ccStripeToken, @Field("isByWizard") long isByWizard);

创建 API:

private void createApi() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        Interceptor headerInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                DeviceInfo info = preferences.deviceInfo.get();
                // Timber.d("Device Info :%s", GSONData.gson().toJson(info));
                Request newRequest;
                String timezone = ManageDTime.getLocalTimeZoneId();
                Timber.d("Timezone %s", timezone);
                if (info.getUserId() == null) {
                    newRequest = chain.request().newBuilder()
                            //.addHeader("Authorization", "Bearer " + Globals.BEARER_TOKEN)
                            .addHeader("deviceid", info.getDeviceId())
                            .addHeader("deviceType", info.getDeviceType())
                            .addHeader("udid", info.getDeviceUniqueId())
                            .addHeader("ver", info.getVersion())
                            .addHeader("build", info.getBuild())
                            .addHeader("os", info.getOsVersion())
                            .addHeader("dname", info.getDeviceName())
                            .addHeader("dmodel", info.getDeviceModel())
                            .addHeader("deviceToken", info.getDeviceToken())
                            .addHeader("cache-control", "no-cache")
                            .addHeader("timezone", timezone)
                            .build();
                } else {
                    User u = preferences.user.get();
                    newRequest = chain.request().newBuilder()
                            .addHeader("Authorization", "Bearer " + u.getToken())
                            .addHeader("userId", info.getUserId())
                            .addHeader("deviceid", info.getDeviceId())
                            .addHeader("deviceType", info.getDeviceType())
                            .addHeader("udid", info.getDeviceUniqueId())
                            .addHeader("ver", info.getVersion())
                            .addHeader("build", info.getBuild())
                            .addHeader("os", info.getOsVersion())
                            .addHeader("dname", info.getDeviceName())
                            .addHeader("dmodel", info.getDeviceModel())
                            .addHeader("deviceToken", info.getDeviceToken())
                            .addHeader("cache-control", "no-cache")
                            .addHeader("timezone", timezone)
                            .build();
                }
                return chain.proceed(newRequest);
            }
        };
        OkHttpClient client = new OkHttpClient.Builder().connectionPool(new ConnectionPool(5, ApiService.TIMEOUT, TimeUnit.SECONDS)).connectTimeout(ApiService.TIMEOUT, TimeUnit.SECONDS).readTimeout(ApiService.TIMEOUT, TimeUnit.SECONDS).writeTimeout(ApiService.TIMEOUT, TimeUnit.SECONDS).addInterceptor(logging).addInterceptor(headerInterceptor).build();
        Retrofit retrofit = new Retrofit.Builder().baseUrl(getApiEndpoint()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).client(client).build();
        api = retrofit.create(ApiService.class);
    }
4

0 回答 0