首页 > 解决方案 > 使用 Retrofit2 POST 时它不起作用

问题描述

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 方法,它将起作用。所以问题可能不在服务器上。

标签: phpandroidretrofitretrofit2

解决方案


推荐阅读