无法使用Retrofit 2.0在post api数据中发送原始json数据[英] Unable to send raw json data in post api data using Retrofit 2.0

本文是小编为大家收集整理的关于无法使用Retrofit 2.0在post api数据中发送原始json数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

无法使用改装2.0

try {
  JSONObject myUserData = new JSONObject();
  myUserData.put("mobile", "917023847899");
  myUserData.put("displayName", "Deepuu");
  myUserData.put("info", "AD");
  myUserData.put("photo", "");
  myUserData.put("displayName", "Deepu");

  JSONObject deviceData = new JSONObject();
  ;
  deviceData.put("deviceId", "2124578910556991");
  deviceData.put("pnToken", "klklklkl");
  deviceData.put("os", "android");
  deviceData.put("targetTopic", "jkjkjkjkj");

  myUserData.put("device", "" + deviceData);
  OkHttpClient client = new OkHttpClient();

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

  Api Ainterface = retrofit.create(Api.class);

  Call<Response> data = Ainterface.getResponse(myUserData);
  data.enqueue(new Callback<Response>() {
    @Override
    public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
      if (response.isSuccessful()) {
        Response rr = response.body();
        Integer ss = rr.getStatusCode();
        Log.e("I Am", "Success"  );
      }
    }

    @Override
    public void onFailure(Call<Response> call, Throwable t) {
      Log.e("I Am", "Failed");
    }
  });

myInterface:

@POST("appUsers/")
Call<Response> getResponse(@Body RequestBody value);

任何人都可以建议使用Retrofit 2.0在POST api中发送原始JSON数据的解决方案?我是新的使用改造;请在onResponse方法中获得null响应,帮助.

推荐答案

您正在使用gsonconverterfactory.因此,您应该通过改装转换为字符串并添加到请求主体的模型类对象.但在你的代码中,你通过了jsonobject.

创建模型类按照项目结构更改包名称:

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Device {

@SerializedName("deviceId")
@Expose
private String deviceId;
@SerializedName("pnToken")
@Expose
private String pnToken;
@SerializedName("os")
@Expose
private String os;
@SerializedName("targetTopic")
@Expose
private String targetTopic;

/**
*
* @return
* The deviceId
*/
public String getDeviceId() {
return deviceId;
}

/**
*
* @param deviceId
* The deviceId
*/
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

/**
*
* @return
* The pnToken
*/
public String getPnToken() {
return pnToken;
}

/**
*
* @param pnToken
* The pnToken
*/
public void setPnToken(String pnToken) {
this.pnToken = pnToken;
}

/**
*
* @return
* The os
*/
public String getOs() {
return os;
}

/**
*
* @param os
* The os
*/
public void setOs(String os) {
this.os = os;
}

/**
*
* @return
* The targetTopic
*/
public String getTargetTopic() {
return targetTopic;
}

/**
*
* @param targetTopic
* The targetTopic
*/
public void setTargetTopic(String targetTopic) {
this.targetTopic = targetTopic;
}

}
-----------------------------------com.example.Request.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Request {

@SerializedName("mobile")
@Expose
private String mobile;
@SerializedName("info")
@Expose
private String info;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("displayName")
@Expose
private String displayName;
@SerializedName("device")
@Expose
private Device device;

/**
*
* @return
* The mobile
*/
public String getMobile() {
return mobile;
}

/**
*
* @param mobile
* The mobile
*/
public void setMobile(String mobile) {
this.mobile = mobile;
}

/**
*
* @return
* The info
*/
public String getInfo() {
return info;
}

/**
*
* @param info
* The info
*/
public void setInfo(String info) {
this.info = info;
}

/**
*
* @return
* The photo
*/
public String getPhoto() {
return photo;
}

/**
*
* @param photo
* The photo
*/
public void setPhoto(String photo) {
this.photo = photo;
}

/**
*
* @return
* The displayName
*/
public String getDisplayName() {
return displayName;
}

/**
*
* @param displayName
* The displayName
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

/**
*
* @return
* The device
*/
public Device getDevice() {
return device;
}

/**
*
* @param device
* The device
*/
public void setDevice(Device device) {
this.device = device;
}

}

和帖子的要求如下:

@POST("yourpath")
    Call<YourResponseModelClass> getResponse(@Body RequestModelClass request);

根据您的请求和响应模型类更新yourResponseModelClass&RequestModelClass.

您可以从json生成模型类, http://www.jsonschema2pojo.org/

其他推荐答案

您可以在复古2.0中发布原始jsonobject.需要将其转换为如此

的要求
RequestBody requestBody =RequestBody.create(MediaType.parse("application/json"),myUserData.toString() );
Call<Response> data = Ainterface.getResponse(requestBody);

本文地址:https://www.itbaoku.cn/post/1937756.html

问题描述

Unable to send raw JSON data in POST api data using Retrofit 2.0

try {
  JSONObject myUserData = new JSONObject();
  myUserData.put("mobile", "917023847899");
  myUserData.put("displayName", "Deepuu");
  myUserData.put("info", "AD");
  myUserData.put("photo", "");
  myUserData.put("displayName", "Deepu");

  JSONObject deviceData = new JSONObject();
  ;
  deviceData.put("deviceId", "2124578910556991");
  deviceData.put("pnToken", "klklklkl");
  deviceData.put("os", "android");
  deviceData.put("targetTopic", "jkjkjkjkj");

  myUserData.put("device", "" + deviceData);
  OkHttpClient client = new OkHttpClient();

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

  Api Ainterface = retrofit.create(Api.class);

  Call<Response> data = Ainterface.getResponse(myUserData);
  data.enqueue(new Callback<Response>() {
    @Override
    public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
      if (response.isSuccessful()) {
        Response rr = response.body();
        Integer ss = rr.getStatusCode();
        Log.e("I Am", "Success"  );
      }
    }

    @Override
    public void onFailure(Call<Response> call, Throwable t) {
      Log.e("I Am", "Failed");
    }
  });

MyInterface:

@POST("appUsers/")
Call<Response> getResponse(@Body RequestBody value);

Can anyone suggest a solution for sending raw JSON data in POST api using Retrofit 2.0? I'm new to using Retrofit; please help as I am getting null response in onResponse method.

推荐答案

You are using GsonConverterFactory. So, you should pass model class object that is converted by retrofit to string and added to request body. But in your code, you passed JSONObject.

Create Model class as follows and change package name according your project structure:

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Device {

@SerializedName("deviceId")
@Expose
private String deviceId;
@SerializedName("pnToken")
@Expose
private String pnToken;
@SerializedName("os")
@Expose
private String os;
@SerializedName("targetTopic")
@Expose
private String targetTopic;

/**
*
* @return
* The deviceId
*/
public String getDeviceId() {
return deviceId;
}

/**
*
* @param deviceId
* The deviceId
*/
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

/**
*
* @return
* The pnToken
*/
public String getPnToken() {
return pnToken;
}

/**
*
* @param pnToken
* The pnToken
*/
public void setPnToken(String pnToken) {
this.pnToken = pnToken;
}

/**
*
* @return
* The os
*/
public String getOs() {
return os;
}

/**
*
* @param os
* The os
*/
public void setOs(String os) {
this.os = os;
}

/**
*
* @return
* The targetTopic
*/
public String getTargetTopic() {
return targetTopic;
}

/**
*
* @param targetTopic
* The targetTopic
*/
public void setTargetTopic(String targetTopic) {
this.targetTopic = targetTopic;
}

}
-----------------------------------com.example.Request.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Request {

@SerializedName("mobile")
@Expose
private String mobile;
@SerializedName("info")
@Expose
private String info;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("displayName")
@Expose
private String displayName;
@SerializedName("device")
@Expose
private Device device;

/**
*
* @return
* The mobile
*/
public String getMobile() {
return mobile;
}

/**
*
* @param mobile
* The mobile
*/
public void setMobile(String mobile) {
this.mobile = mobile;
}

/**
*
* @return
* The info
*/
public String getInfo() {
return info;
}

/**
*
* @param info
* The info
*/
public void setInfo(String info) {
this.info = info;
}

/**
*
* @return
* The photo
*/
public String getPhoto() {
return photo;
}

/**
*
* @param photo
* The photo
*/
public void setPhoto(String photo) {
this.photo = photo;
}

/**
*
* @return
* The displayName
*/
public String getDisplayName() {
return displayName;
}

/**
*
* @param displayName
* The displayName
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
}

/**
*
* @return
* The device
*/
public Device getDevice() {
return device;
}

/**
*
* @param device
* The device
*/
public void setDevice(Device device) {
this.device = device;
}

}

And request of post as follows:

@POST("yourpath")
    Call<YourResponseModelClass> getResponse(@Body RequestModelClass request);

Update YourResponseModelClass & RequestModelClass according to your request and response model class.

You can generate model class from json, http://www.jsonschema2pojo.org/

其他推荐答案

you can post raw jsonObject in retro 2.0. need to convert that into RequestBody like this

RequestBody requestBody =RequestBody.create(MediaType.parse("application/json"),myUserData.toString() );
Call<Response> data = Ainterface.getResponse(requestBody);