OkHttp POST 似乎没有发送请求正文
OkHttp POST Does Not Seem to Send Request Body
我正在尝试使用 RetroFit2
和 OkHttp3
在 PHP 脚本上执行 POST 方法。我一直在很好地执行 GET 方法,但我在发布时遇到问题。
我用 OkHttpClient
设置了一个 HttpLoggingInterceptor
,它完美地记录了请求正文。但是我的 PHP 脚本没有收到数据,所以我尝试在收到数据后立即输出 $_POST
数据,但它是一个空字符串。我无法判断 Android 端、服务器端或两者是否有问题。
我的 RetroFit
对象和 OkHttpClient
是这样设置的:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(RestCookieJar.getInstance())
.addInterceptor(interceptor)
.build();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Instant.class, new InstantAdapter())
.registerTypeAdapter(LatLng.class, new LatLngAdapter())
.registerTypeAdapter(Uri.class, new UriAdapter())
.create();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.baseUrl(BASE_URL)
.build();
RestService service = retrofit.create(RestService.class);
然后我的RestService
界面是这样设置的:
public interface RestService {
@GET("api/index.php")
Call<List<Session>> getSessions();
@POST("api/index.php")
Call<ResponseBody> postSession(@Body Session session);
}
正如我所提到的,这一切 似乎 都有效,但是当我调用时:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
print_r($_POST);
}
我得到一组空括号。
我在这里找到了答案:php $_POST array empty upon form submission. Retrofit automatically sets the Content-Type
to "application/json; charset=UTF-8"
, which in PHP versions 5.0 - 5.19, there is a bug 导致请求正文未被解析为 $_POST
变量。我使用的服务器是 运行 PHP 5.10(我无法控制)。
此错误的解决方法是自己从原始请求正文中解析 JSON 数据:
if($_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') {
$_POST = json_decode(file_get_contents('php://input'));
}
我正在尝试使用 RetroFit2
和 OkHttp3
在 PHP 脚本上执行 POST 方法。我一直在很好地执行 GET 方法,但我在发布时遇到问题。
我用 OkHttpClient
设置了一个 HttpLoggingInterceptor
,它完美地记录了请求正文。但是我的 PHP 脚本没有收到数据,所以我尝试在收到数据后立即输出 $_POST
数据,但它是一个空字符串。我无法判断 Android 端、服务器端或两者是否有问题。
我的 RetroFit
对象和 OkHttpClient
是这样设置的:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(RestCookieJar.getInstance())
.addInterceptor(interceptor)
.build();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Instant.class, new InstantAdapter())
.registerTypeAdapter(LatLng.class, new LatLngAdapter())
.registerTypeAdapter(Uri.class, new UriAdapter())
.create();
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.baseUrl(BASE_URL)
.build();
RestService service = retrofit.create(RestService.class);
然后我的RestService
界面是这样设置的:
public interface RestService {
@GET("api/index.php")
Call<List<Session>> getSessions();
@POST("api/index.php")
Call<ResponseBody> postSession(@Body Session session);
}
正如我所提到的,这一切 似乎 都有效,但是当我调用时:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
print_r($_POST);
}
我得到一组空括号。
我在这里找到了答案:php $_POST array empty upon form submission. Retrofit automatically sets the Content-Type
to "application/json; charset=UTF-8"
, which in PHP versions 5.0 - 5.19, there is a bug 导致请求正文未被解析为 $_POST
变量。我使用的服务器是 运行 PHP 5.10(我无法控制)。
此错误的解决方法是自己从原始请求正文中解析 JSON 数据:
if($_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') {
$_POST = json_decode(file_get_contents('php://input'));
}