Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何参考API的shell命令向API发出POST请求?_Android_Shell_Api_Retrofit2_Hypertrack - Fatal编程技术网

Android 如何参考API的shell命令向API发出POST请求?

Android 如何参考API的shell命令向API发出POST请求?,android,shell,api,retrofit2,hypertrack,Android,Shell,Api,Retrofit2,Hypertrack,我正在开发使用API的Android应用程序(这是一个web服务,提供其API用于实时跟踪移动设备。它还提供单独API中的任务和驱动程序处理功能。) 有一些功能要求我使用下面的shell命令获取驱动程序密钥: Driver API: curl -H "Authorization: token YOUR_SK_TOKEN" \ -H "Content-Type: application/json" \ -X POST \ -d "{\"name\": \"Test driver\", \"v

我正在开发使用API的Android应用程序(这是一个web服务,提供其API用于实时跟踪移动设备。它还提供单独API中的任务和驱动程序处理功能。)

有一些功能要求我使用下面的shell命令获取驱动程序密钥:

Driver API:
curl -H "Authorization: token YOUR_SK_TOKEN" \
 -H "Content-Type: application/json" \
 -X POST \
 -d "{\"name\": \"Test driver\", \"vehicle_type\": \"car\"}" \
 https://app.hypertrack.io/api/v1/drivers/
这就是我如何使用带有以下请求接口的Reformation2实现这两个API的方法:

public interface DriverRequestInterface
{
    @Headers ({
        "Authorization: token SECRET_KEY",
        "Content-Type: application/json"
    })
    @POST ( "api/v1/drivers" )
    Call<DriverJSONResponse> getJSON (@Body DriverJSONResponse jsonResponse);
}
到目前为止,我收到的是GET回复,而不是帖子。我正在接收带有结果列表的JSON对象,但无法将任何内容发布到API


如何参考API的shell命令向API发出POST请求?

由于
DriverJSONResponse
类包含其他字段以及
name
vehicle\u type
,上述代码将以下数据传递给
POST

{"count":0, "name":"Brian", "vehicle_type":"car", "results":[] }
这将导致JSON解析错误

因此,使用另一个模型类来传递
POST
参数,如:

public class DriverJSON
{
    @SerializedName ( "name" ) private String name;
    @SerializedName ( "vehicle_type" ) private String vehicleType;

    public DriverJSON(String name, String vehicleType)
    {
        this.name = name;
        this.vehicleType = vehicleType;
    }

    public String getName () { return name; }
    public String getVehicleType () { return vehicleType; }
}
并在
RequestInterface
中传递此模型类,如下所示:

public interface DriverRequestInterface
{
    @Headers ({
        "Authorization: token YOUR_SECRET_KEY",
        "Content-Type: application/json"
    })
    @POST ( "api/v1/drivers/" )
    Call<DriverJSONResponse> getJSON (@Body DriverJSON json);
}
公共接口驱动程序请求接口
{
@标题({
“授权:标记您的\u密钥”,
“内容类型:应用程序/json”
})
@POST(“api/v1/drivers/”)
调用getJSON(@Body DriverJSON);
}
别忘了根据您希望接收的JSON对象对您的
DriverJSONResponse
进行建模

public interface DriverRequestInterface
{
    @Headers ({
        "Authorization: token YOUR_SECRET_KEY",
        "Content-Type: application/json"
    })
    @POST ( "api/v1/drivers/" )
    Call<DriverJSONResponse> getJSON (@Body DriverJSON json);
}