Retrofit rt;
rt=new Retrofit.Builder().baseUrl("http://10.0.2.2/").addConverterFactory(ScalarsConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();
Foo foo=rt.create(Foo.class);
//Map<String,String> mapp=new HashMap<>();
//mapp.put("author","author");
Call<String> msg=foo.postJson("msg");
msg.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.v("STERLITAMAK", response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.v("STERLITAMAK", t.toString());
}
});
接口代码
public interface Foo {
@GET("Singleton")
Call<String> getJson(@Query("weneed") String author);
@POST("Singleton")
@FormUrlEncoded
Call<String> postJson(@Field("weneed") String author);
@FormUrlEncoded
@POST("Singleton")
Call<String> postJson1(@FieldMap Map<String,String> data);
}
服务器上的PHP代码
echo $_POST['weneed'];
问题是,当我运行代码时,服务器只是看不到POST请求。但是答案是正确的。如果我使用接口的getJson方法,它将起作用。所以问题可能不在服务器上。