如何使用java sdk将资产上载到usergrid api

如何使用java sdk将资产上载到usergrid api,java,usergrid,Java,Usergrid,我不熟悉api的用户网格。但是,我必须通过代码(Java)测试usergrid。有人能发布一个方法,使用Java客户端将资产(文件)上传到usergrid中吗。经过一番努力,我找到了一种方法。这是 public static ApiResponse uploadFileToServer(){ File file = new File("your asset path"); Map<String, Object> dataParams = new HashMap

我不熟悉api的用户网格。但是,我必须通过代码(Java)测试usergrid。有人能发布一个方法,使用Java客户端将资产(文件)上传到usergrid中吗。

经过一番努力,我找到了一种方法。这是

    public static ApiResponse uploadFileToServer(){
    File file = new File("your asset path");
    Map<String, Object> dataParams = new HashMap<String, Object>();
    data.put("name", <file_name>);
    data.put("file", file);
    //here type and name are necessary to create entity
    data.put("type", <collection_name>);
    data.put("name", <entity_name>);
    //entity_name will be the name of your new created entity.
    Client client = new Client(<organization_name>, <application_name>);
    client.setApiUrl(<url to hit>);
    client.authorizeAppUser(<username>, <password>);
    client.createEntity(dataParams);
    String <uri> = <collection_name> + "/" + <entity_name>;
    ApiResponse response = client.httpRequest(HttpMethod.POST,ApiResponse.class, null, dataParams, <organization_name>, <application_name>,
            <uri>);
    return response;
    }
公共静态ApiResponse uploadFileToServer(){
文件=新文件(“您的资产路径”);
Map dataParams=new HashMap();
数据。放入(“名称”);
data.put(“文件”,file);
//此处类型和名称是创建实体所必需的
数据。put(“类型”);
数据。放入(“名称”);
//实体名称将是新创建实体的名称。
客户=新客户(,);
client.setapirl();
client.authorizeAppUser(,);
client.createEntity(数据参数);
字符串=+“/”+;
ApiResponse response=client.httpRequest(HttpMethod.POST,ApiResponse.class,null,dataParams,
);
返回响应;
}

您不需要通过读取来序列化文件,因为请求将为您实现这一点。