【发布时间】:2016-07-30 06:22:22
【问题描述】:
我正在尝试使用 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 android post retrofit2 okhttp3