Android 我怎样才能得到;nameValuePairs“;在JSON请求中使用改型?
我如何发布一个像下面这样的Android 我怎样才能得到;nameValuePairs“;在JSON请求中使用改型?,android,json,retrofit2,Android,Json,Retrofit2,我如何发布一个像下面这样的JSONObject请求 原始请求: 在截击中,我可以成功地发布JSONObject请求。但当我在改装中使用此功能时,我的请求更改如下: 我在日志中得到了什么: 我收到来自服务器端的错误请求 我的改装API接口: 主要活动代码: 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendPo
JSONObject
请求
原始请求:
在截击中,我可以成功地发布JSONObject
请求。但当我在改装中使用此功能时,我的请求更改如下:
我在日志中得到了什么:
我收到来自服务器端的错误请求
我的改装API接口:
主要活动代码:
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
公共void sendPost(JSONObject requestobject){
Log.e(“requestobject”,requestobject.toString());
Call Call=mAPIService.savePost(requestobject);
call.enqueue(新回调(){
@凌驾
public void onResponse(Callcallback、Responseresponse){
String res=response.body();
Log.e(“演示”、“提交给API的帖子”+“回复”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“演示”,“无法向API提交帖子”,t);
Log.e(“调用”,String.valueOf(调用));
}
});
}
注意:我也尝试过使用
HashMap
但是自动添加了相同的nameValuePairs
参数。如果我使用GsonJsonObject
,那么请求序列化转换。所以我不想对服务器端进行任何更改,因为使用截击可以很好地工作。但现在我想使用相同的请求进行改装。更改我的APi接口进行改装
public interface ApiInterface {
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("re_clientdata")
Call<String> savePost(@Body RequestBody req);
}
公共接口{
@标题(“内容类型:application/json;字符集=utf-8”)
@POST(“re_clientdata”)
调用savePost(@Body RequestBody req);
}
还可以这样更改主活动代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
public void sendPost(JSONObject requestobject) {
Log.e("requestobject",requestobject.toString());
RequestBody myreqbody = null;
try {
myreqbody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
(new JSONObject(String.valueOf(requestobject))).toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<String> call =mAPIService.savePost(myreqbody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String>callback,Response<String>response) {
String res = response.body();
Log.e("DEMO", "post submitted to API." + response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("DEMO", "Unable to submit post to API.",t);
Log.e("call", String.valueOf(call));
}
});
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
公共void sendPost(JSONObject requestobject){
Log.e(“requestobject”,requestobject.toString());
RequestBody myreqbody=null;
试一试{
myreqbody=RequestBody.create(okhttp3.MediaType.parse(“application/json;charset=utf-8”),
(新的JSONObject(String.valueOf(requestobject))).toString();
}捕获(JSONException e){
e、 printStackTrace();
}
Call Call=mAPIService.savePost(myreqbody);
call.enqueue(新回调(){
@凌驾
public void onResponse(Callcallback、Responseresponse){
String res=response.body();
Log.e(“演示”、“提交给API的帖子”+“回复”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“演示”,“无法向API提交帖子”,t);
Log.e(“调用”,String.valueOf(调用));
}
});
}
有关更多详细信息,请查看此
建议
Pratik Vyas
更改My APi接口以进行改装
public interface ApiInterface {
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("re_clientdata")
Call<String> savePost(@Body RequestBody req);
}
公共接口{
@标题(“内容类型:application/json;字符集=utf-8”)
@POST(“re_clientdata”)
调用savePost(@Body RequestBody req);
}
还可以这样更改主活动代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
public void sendPost(JSONObject requestobject) {
Log.e("requestobject",requestobject.toString());
RequestBody myreqbody = null;
try {
myreqbody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
(new JSONObject(String.valueOf(requestobject))).toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<String> call =mAPIService.savePost(myreqbody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String>callback,Response<String>response) {
String res = response.body();
Log.e("DEMO", "post submitted to API." + response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("DEMO", "Unable to submit post to API.",t);
Log.e("call", String.valueOf(call));
}
});
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
公共void sendPost(JSONObject requestobject){
Log.e(“requestobject”,requestobject.toString());
RequestBody myreqbody=null;
试一试{
myreqbody=RequestBody.create(okhttp3.MediaType.parse(“application/json;charset=utf-8”),
(新的JSONObject(String.valueOf(requestobject))).toString();
}捕获(JSONException e){
e、 printStackTrace();
}
Call Call=mAPIService.savePost(myreqbody);
call.enqueue(新回调(){
@凌驾
public void onResponse(Callcallback、Responseresponse){
String res=response.body();
Log.e(“演示”、“提交给API的帖子”+“回复”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“演示”,“无法向API提交帖子”,t);
Log.e(“调用”,String.valueOf(调用));
}
});
}
有关更多详细信息,请查看此
建议
Pratik Vyas
试试这个解决方案,也许它会帮你。欢迎mate@a。请尝试此解决方案,它可能会对您有所帮助。欢迎mate@a.dev
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
public void sendPost(JSONObject requestobject) {
Log.e("requestobject",requestobject.toString());
Call<String> call =mAPIService.savePost(requestobject);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String>callback,Response<String>response) {
String res = response.body();
Log.e("DEMO", "post submitted to API." + response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("DEMO", "Unable to submit post to API.",t);
Log.e("call", String.valueOf(call));
}
});
}
public interface ApiInterface {
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("re_clientdata")
Call<String> savePost(@Body RequestBody req);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendPost(jRequest);
}
public void sendPost(JSONObject requestobject) {
Log.e("requestobject",requestobject.toString());
RequestBody myreqbody = null;
try {
myreqbody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
(new JSONObject(String.valueOf(requestobject))).toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<String> call =mAPIService.savePost(myreqbody);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String>callback,Response<String>response) {
String res = response.body();
Log.e("DEMO", "post submitted to API." + response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("DEMO", "Unable to submit post to API.",t);
Log.e("call", String.valueOf(call));
}
});
}