Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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上调用PostAPI_Android_Api_Android Studio - Fatal编程技术网

在android上调用PostAPI

在android上调用PostAPI,android,api,android-studio,Android,Api,Android Studio,在Android中,我试图请求一个简单的POST-API,该API使用一个简单的JSON对象进行响应 比如说,我有一个简单的API(1.1.1.1/testApi),它与包含以下内容的JSON对象进行响应: 状态:状态值 名称:名称值 工作起来很有魅力,所以我认为我的API很好 我已经尝试了下面的一些链接: :没有关于如何调用CallApi对象和解析API地址(例如URL)的示例,因此在尝试调用该对象时总是会出现错误 :正如链接所说,Android 6.0几乎不推荐所有答案 :似乎可用,但我

在Android中,我试图请求一个简单的POST-API,该API使用一个简单的JSON对象进行响应

比如说,我有一个简单的API(1.1.1.1/testApi),它与包含以下内容的JSON对象进行响应:

  • 状态:状态值
  • 名称:名称值
工作起来很有魅力,所以我认为我的API很好

我已经尝试了下面的一些链接:

  • :没有关于如何调用
    CallApi
    对象和解析API地址(例如URL)的示例,因此在尝试调用该对象时总是会出现错误
  • :正如链接所说,Android 6.0几乎不推荐所有答案
  • :似乎可用,但我找不到合适的示例在我的案例中使用此选项
  • 我确实花了时间来搜索有关此的解决方案,但恐怕没有“简单”的方法来调用POST-API

    有没有简单的方法可以接受URL输入,然后返回JSON对象

    如果这是一个重复的问题,请告诉我


    提前谢谢。

    拉赫马特。如果您希望向Web API发送POST请求,可以尝试使用Android Volley库。你可以参考下面的链接

    安卓截击库

    辅导


    拉赫马特。如果您希望向Web API发送POST请求,可以尝试使用Android Volley库。你可以参考下面的链接

    安卓截击库

    辅导


    您可以使用RestTemplate使用Restful服务,这非常简单。下面是一个示例代码,我在其中发布了一个对象

    public MasterObject setMasterByBatch(MasterObject masterObject) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            masterObject = restTemplate.postForObject(yourUrl, masterObject, MasterObject.class);          
    
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("masterObjPost_WsCli_EX", e.toString());
        }
        return masterObject;
    }
    
    这在您的build.gradle(模块:app)中需要很少的依赖项:


    如果显示有关org.springframework的任何错误,您可能需要下载并插入spring库

    您可以使用rest模板使用Restful服务,这非常简单。下面是一个示例代码,我在其中发布了一个对象

    public MasterObject setMasterByBatch(MasterObject masterObject) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    
            masterObject = restTemplate.postForObject(yourUrl, masterObject, MasterObject.class);          
    
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("masterObjPost_WsCli_EX", e.toString());
        }
        return masterObject;
    }
    
    public void getTaskData(){
            ServiceInterface serviceInterface=ServiceClass.connection();
            Call<ListData> call=serviceInterface.taskData("getAllUsersSimple",0);
            call.enqueue(new Callback<ListData>() {
                @Override
                public void onResponse(Response<ListData> response, Retrofit retrofit) {
                    Log.v("@@@Response",""+response.toString());
                    if(response.isSuccess()){
                        listData=response.body();
                        dataList=listData.getData();
                        printStudentDetails(dataList);
    
                    }
                }
    
                @Override
                public void onFailure(Throwable t) {
                    Log.v("@@@Failure"," Message"+t.getMessage());
                }
            });
        }
    
    这在您的build.gradle(模块:app)中需要很少的依赖项:


    如果显示有关org.springframework的任何错误,您可能需要下载并插入spring库

    我个人更喜欢改型,它非常容易使用

    public void getTaskData(){
            ServiceInterface serviceInterface=ServiceClass.connection();
            Call<ListData> call=serviceInterface.taskData("getAllUsersSimple",0);
            call.enqueue(new Callback<ListData>() {
                @Override
                public void onResponse(Response<ListData> response, Retrofit retrofit) {
                    Log.v("@@@Response",""+response.toString());
                    if(response.isSuccess()){
                        listData=response.body();
                        dataList=listData.getData();
                        printStudentDetails(dataList);
    
                    }
                }
    
                @Override
                public void onFailure(Throwable t) {
                    Log.v("@@@Failure"," Message"+t.getMessage());
                }
            });
        }
    

    就我个人而言,我更喜欢改装,它非常简单,使用起来也非常好


    您好,我有一个工作改装示例,请以您的方式试用

    让我们开始吧

    1) Gradle

        compile 'com.google.code.gson:gson:2.2.4'
        compile 'com.squareup.okhttp:okhttp:2.4.0'
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
        compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' 
    
    public interface ServiceInterface {
    @GET(HttpConstants.USERDATAJSON)
        Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
    }
    
    2) 接口

        compile 'com.google.code.gson:gson:2.2.4'
        compile 'com.squareup.okhttp:okhttp:2.4.0'
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
        compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' 
    
    public interface ServiceInterface {
    @GET(HttpConstants.USERDATAJSON)
        Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
    }
    
    4) 从活动调用方法

    public void getTaskData(){
            ServiceInterface serviceInterface=ServiceClass.connection();
            Call<ListData> call=serviceInterface.taskData("getAllUsersSimple",0);
            call.enqueue(new Callback<ListData>() {
                @Override
                public void onResponse(Response<ListData> response, Retrofit retrofit) {
                    Log.v("@@@Response",""+response.toString());
                    if(response.isSuccess()){
                        listData=response.body();
                        dataList=listData.getData();
                        printStudentDetails(dataList);
    
                    }
                }
    
                @Override
                public void onFailure(Throwable t) {
                    Log.v("@@@Failure"," Message"+t.getMessage());
                }
            });
        }
    
    public void getTaskData(){
    ServiceInterface ServiceInterface=ServiceClass.connection();
    Call Call=serviceInterface.taskData(“getAllUsersSimple”,0);
    call.enqueue(新回调(){
    @凌驾
    公共响应(响应、改装){
    Log.v(“@@@Response”和“+Response.toString());
    if(response.issucess()){
    listData=response.body();
    dataList=listData.getData();
    printStudentDetails(数据列表);
    }
    }
    @凌驾
    失效时的公共无效(可丢弃的t){
    Log.v(“@@@Failure”,“Message”+t.getMessage());
    }
    });
    }
    
    5) Pojo

    public class ListData {
    
        @SerializedName("data")
        @Expose
        private List<DataPojo> data = null;
        @SerializedName("code")
        @Expose
        private Integer code;
        @SerializedName("message")
        @Expose
        private String message;
    
        public List<DataPojo> getData() {
            return data;
        }
    
        public void setData(List<DataPojo> data) {
            this.data = data;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    }
    public class DataPojo {
    
        @SerializedName("user_id")
        @Expose
        private String userId;
        @SerializedName("user_name")
        @Expose
        private String userName;
        @SerializedName("user_age")
        @Expose
        private String userAge;
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserAge() {
            return userAge;
        }
    
        public void setUserAge(String userAge) {
            this.userAge = userAge;
        }
    
    }
    
    公共类ListData{
    @SerializedName(“数据”)
    @暴露
    私有列表数据=null;
    @序列化名称(“代码”)
    @暴露
    专用整数码;
    @SerializedName(“消息”)
    @暴露
    私有字符串消息;
    公共列表getData(){
    返回数据;
    }
    公共无效设置数据(列表数据){
    这个数据=数据;
    }
    公共整数getCode(){
    返回码;
    }
    公共无效设置码(整数码){
    this.code=代码;
    }
    公共字符串getMessage(){
    返回消息;
    }
    公共无效设置消息(字符串消息){
    this.message=消息;
    }
    }
    公共类DataPojo{
    @SerializedName(“用户id”)
    @暴露
    私有字符串用户标识;
    @SerializedName(“用户名”)
    @暴露
    私有字符串用户名;
    @SerializedName(“用户年龄”)
    @暴露
    私有字符串用户年龄;
    公共字符串getUserId(){
    返回用户标识;
    }
    public void setUserId(字符串userId){
    this.userId=userId;
    }
    公共字符串getUserName(){
    返回用户名;
    }
    public void setUserName(字符串用户名){
    this.userName=用户名;
    }
    公共字符串getUserAge(){
    返回用户年龄;
    }
    public void setUserAge(字符串userAge){
    this.userAge=userAge;
    }
    }
    
    您可以使用此链接创建pojo

    更多参考请访问链接

    您好,我有一个工作改装示例,请以您的方式试用

    让我们开始吧

    1) Gradle

        compile 'com.google.code.gson:gson:2.2.4'
        compile 'com.squareup.okhttp:okhttp:2.4.0'
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
        compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' 
    
    public interface ServiceInterface {
    @GET(HttpConstants.USERDATAJSON)
        Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
    }
    
    2) 接口

        compile 'com.google.code.gson:gson:2.2.4'
        compile 'com.squareup.okhttp:okhttp:2.4.0'
        compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
        compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
        compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' 
    
    public interface ServiceInterface {
    @GET(HttpConstants.USERDATAJSON)
        Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
    }
    
    4) 从活动调用方法

    public void getTaskData(){
            ServiceInterface serviceInterface=ServiceClass.connection();
            Call<ListData> call=serviceInterface.taskData("getAllUsersSimple",0);
            call.enqueue(new Callback<ListData>() {
                @Override
                public void onResponse(Response<ListData> response, Retrofit retrofit) {
                    Log.v("@@@Response",""+response.toString());
                    if(response.isSuccess()){
                        listData=response.body();
                        dataList=listData.getData();
                        printStudentDetails(dataList);
    
                    }
                }
    
                @Override
                public void onFailure(Throwable t) {
                    Log.v("@@@Failure"," Message"+t.getMessage());
                }
            });
        }
    
    public void getTaskData(){
    ServiceInterface ServiceInterface=ServiceClass.connection();
    Call Call=serviceInterface.taskData(“getAllUsersSimple”,0);
    call.enqueue(新回调(){
    @凌驾
    公共响应(响应、改装){
    Log.v(“@@@Response”和“+Response.toString());
    if(response.issucess()){
    listData=response.body();
    dataList=listData.getData();
    printStudentDetails(数据列表);
    }
    }
    @凌驾
    失效时的公共无效(可丢弃的t){
    Log.v(“@@@Failure”,“Message”+t.getMessage());
    }
    });
    }
    
    5) Pojo

    public class ListData {
    
        @SerializedName("data")
        @Expose
        private List<DataPojo> data = null;
        @SerializedName("code")
        @Expose
        private Integer code;
        @SerializedName("message")
        @Expose
        private String message;
    
        public List<DataPojo> getData() {
            return data;
        }
    
        public void setData(List<DataPojo> data) {
            this.data = data;
        }
    
        public Integer getCode() {
            return code;
        }
    
        public void setCode(Integer code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
    }
    public class DataPojo {
    
        @SerializedName("user_id")
        @Expose
        private String userId;
        @SerializedName("user_name")
        @Expose
        private String userName;
        @SerializedName("user_age")
        @Expose
        private String userAge;
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserAge() {
            return userAge;
        }
    
        public void setUserAge(String userAge) {
            this.userAge = userAge;
        }
    
    }
    
    公共类ListData{
    @SerializedName(“数据”)
    @暴露
    私有列表数据=null;
    @序列化名称(“代码”)
    @暴露
    专用整数码;
    @SerializedName(“消息”)