相关疑难解决方法(0)

Android Retrofit:内容类型为application/x-www-form-urlencoded

Android开发相当新颖.我正在尝试使用改造来发送帖子请求.在我的改造日志中,我看到了

Content-Type: text/plain; charset=utf-8
Run Code Online (Sandbox Code Playgroud)

我发现请求只有在我使用内容类型时才有效:

application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)

我搜索了谷歌,发现没有明确的方法来明确设置内容类型.谁知道怎么做?

post android retrofit

43
推荐指数
2
解决办法
3万
查看次数

使用Retrofit2在POST请求中发送JSON

我正在使用Retrofit来集成我的Web服务,我不明白如何使用POST请求将JSON对象发送到服务器.我目前卡住了,这是我的代码:

活动:-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Retrofit retrofit = new Retrofit.Builder().baseUrl(url).
            addConverterFactory(GsonConverterFactory.create()).build();

    PostInterface service = retrofit.create(PostInterface.class);

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("email", "device3@gmail.com");
        jsonObject.put("password", "1234");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    final String result = jsonObject.toString();

}
Run Code Online (Sandbox Code Playgroud)

PostInterface: -

public interface PostInterface {

    @POST("User/DoctorLogin")
    Call<String> getStringScalar(@Body String body);
}
Run Code Online (Sandbox Code Playgroud)

请求JSON: -

{
"email":"device3@gmail.com",
"password":"1234"
}
Run Code Online (Sandbox Code Playgroud)

响应JSON: -

{
  "error": false,
  "message": "User Login Successfully",
  "doctorid": 42,
  "active": true
}
Run Code Online (Sandbox Code Playgroud)

android json retrofit2

9
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×2

json ×1

post ×1

retrofit ×1

retrofit2 ×1