当前位置: 首页 > 知识库问答 >
问题:

改装,Gson,将数据从应用程序自定义jsonfile解析到服务器

羊舌新荣
2023-03-14

I'am trying to work with reformation to parse custom json data with Gson to a localhost server using a REST API。

这是生成的json im正在寻找:

{
"user_id": 2,
"product": {
    "id": 15,
    "item_count": 99
    }
}

下面是我创建的界面

public interface APIService {

@POST("/products")

@FormUrlEncoded
Call<Product> savePost(@Header("Token")String authKey,
                    @Field("product") Object product,
                    @Field("userId") String userId);
}

这是我用来发送数据的函数

// string that i use to call the classes

        String userID;
        String authKey;

        mAPIService = ApiUtils.getAPIService();
        mProduct = new Product();
        mProduct.setId(productID);
        mProduct.setItemCount(numberOfItems.getText().toString().trim());
        final String stringMProduct = mProduct.toString();

        sendPost(userID, stringMProduct, authKey);

// the function that parses all the data

public void sendPost( String userID, String product, String authKey ) {
    mAPIService.savePost(authKey, userID, product).enqueue(new Callback<Product>() {

        @Override
        public void onResponse(Call<Product> call, retrofit2.Response<Product> response) {
            if(response.isSuccessful()) {
                showResponse(response.body().toString());
                Log.i(TAG, "post submitted to API." + response.body().toString());
            }
        }

        @Override
        public void onFailure(Call<Product> call, Throwable t) {
            Log.e(TAG, "Unable to submit post to API.");
        }
    });
}

这是我使用Gson将数据解析为json的类

public class OrderRequest {

     @SerializedName("product")
     @Expose
     private Product product;
     @SerializedName("user_id")
     @Expose
     private String userId;

     public Product getProduct() {
         return product;
     }

     public void setProduct(Product product) {
         this.product = product;
     }

     public String getUserId() {
         return userId;
     }

     public void setUserId(String userId) {
         this.userId = userId;
     }

     @Override

     public String toString() {
         return "OrderRequest{" +
                 "product=" + product +
                 ", userId='" + userId + '\'' +
        '}';
    }
}

另一个Gcode类。

Public class Product {

    @SerializedName("item_count")
    @Expose
    private String itemCount;
    @SerializedName("id")
    @Expose
    private String id;

    public String getItemCount() {
        return itemCount;
    }

    public void setItemCount(String itemCount) {
        this.itemCount = itemCount;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Product{" +
                "itemCount='" + itemCount + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}

这是我用来解析数据到服务器的所有代码,我真的很困惑我应该如何调试它或者有效负载在哪里,我知道这是一个代码转储,这是不允许的。我最困惑的一点是让海关邮政请求您可以在数组中拥有数组的位

共有2个答案

韩阳云
2023-03-14

首先,您要修改的url是什么?看起来你的设定有点像。。"http://something.com/something“.Reformation要求您使用的url应以“/”结尾。我可以判断,因为您的发布方法是“/products”。如果您正确设置url,则会是”http://something.com/something//products"

不仅如此。@formURLEncoded不能与post方法一起使用。对于那个@field,它也会并且应该工作...

司毅庵
2023-03-14

对于数组和其他类型的数据,您可以在请求体和服务器端发送数据,您可以轻松地将其映射到您正在创建的POJO类到POJO类,这与您正在发送的POJO类相同。

 类似资料:
  • 先生们,你能帮我改造吗?我正在尝试实施Post请求,但我不知道如何正确执行。 类SaleEvent与JsonArray“更新”中的json对象具有相同的字段问题是-我有复杂的响应,不容易为gson解析: 我需要的是在阵列列表中得到回应 就像这样 问题已解决:问题在于使用ArrayList而不是List

  • 我有一个不能用Gson正确序列化的类(类只是name和HashMap),所以我编写了一个自定义序列化程序来从HashMap中打印名称和键值对。 此外,这是实际打印的内容,序列化整个对象并不像我所期望的那样工作,也不只是直接打印对象。 任何帮助都将不胜感激。

  • 我已使用WebForms从VS-2019成功发布Azure应用服务。我已成功保护它,以便用户必须使用与应用服务相同域中的Azure AAD帐户登录。我已成功创建AzureSQL数据库。我已成功将AAD域中的用户添加到数据库中,并通过将我创建的Azure AAD帐户用户之一硬编码到连接字符串中,从Azure应用服务中连接到数据库。 现在,我想使用应用程序服务登录名中经过身份验证的AAD用户连接到Az

  • 您好,我有这个代码,我想将此数据解析为对象,现在我得到了一个 anyType 的字符串 我想分别得到描述对象纬度对象和经度对象

  • 我正在探索将java web应用程序移动到Azure应用程序服务的可能性。应用程序on prem在启动时读取属性文件。 是否有可能将属性文件传递或放置到应用服务?如果没有,建议将此类遗留应用程序移动到Azure应用服务?