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)
我搜索了谷歌,发现没有明确的方法来明确设置内容类型.谁知道怎么做?
我正在使用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)
public interface PostInterface {
@POST("User/DoctorLogin")
Call<String> getStringScalar(@Body String body);
}
Run Code Online (Sandbox Code Playgroud)
{
"email":"device3@gmail.com",
"password":"1234"
}
Run Code Online (Sandbox Code Playgroud)
{
"error": false,
"message": "User Login Successfully",
"doctorid": 42,
"active": true
}
Run Code Online (Sandbox Code Playgroud)