在 android 上调用 POST API [英] Calling a POST API on android
问题描述
我正在尝试从 Android 请求一个简单的 POST-API,该 API 以一个简单的 JSON 对象进行响应.
From Android, I'm trying to request a simple POST-API that responses with a simple JSON object.
比如说,我有一个简单的 API (1.1.1.1/testApi),它响应一个包含以下内容的 JSON 对象:
Say, I have a simple API (1.1.1.1/testApi) that respons with a JSON object that contains:
- 状态:状态值
- 名称:名称值
使用 Postman 调用 API 很有魅力,所以我假设我的API 很好.
Calling the API using Postman works like a charm, so I assume that my API was fine.
我已经尝试过下面的一些链接:
I already tried some of the links below:
- AsyncTask:没有关于如何调用
CallApi 的示例
对象并解析 API 地址(例如 URL),所以当我尝试调用该对象时总是会出错. - Apache HTTP Client:正如链接所说,几乎所有的答案都是不推荐用于 Android 6.0
- Retrofit:似乎可用,但我找不到合适的示例在我的案例
- AsyncTask: there is no example on how to call the
CallApi
object and parse the API address (e.g. URL), so there is always an error when I try to invoke the object. - Apache HTTP Client: as the link said, nearly all of the answer are deprecated for Android 6.0
- Retrofit: seems usable, but I can't find a proper example to use this in my case
我确实花时间搜索了有关此问题的解决方案,但我认为没有简单"的方法可以调用 POST-API.
I did take my time to search solutions regarding this, but afaik there is no "easy" way to call a POST-API.
是否有任何简单的方法可以接受 URL 输入,然后返回一个 JSON 对象?
Is there any simple method that takes an URL input, then returns a JSON object?
如果这是一个重复的问题,请告诉我.
Let me know if this was a duplicated question.
提前致谢.
推荐答案
Hello I Have working Retrofit Example 按照你的方式试试
Hello I Have working Retrofit Example try it on your manner
开始吧
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'
2) 界面
public interface ServiceInterface {
@GET(HttpConstants.USERDATAJSON)
Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
}
3) 服务类
public class ServiceClass {
static ServiceInterface serviceInterface;
// public static final String baseUrl= HttpConstants.BASE_URL_GEONAME;
public static final String baseUrl= HttpConstants.baseUrl;
public static ServiceInterface connection(){
if(serviceInterface==null){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response=chain.proceed(chain.request());
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build();
serviceInterface=retrofit.create(ServiceInterface.class);
}
return serviceInterface;
}
}
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());
}
});
}
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;
}
}
您可以使用此链接创建您的 pojohttp://www.jsonschema2pojo.org/
You can create your pojo using this link http://www.jsonschema2pojo.org/
更多参考请访问链接https://github.com/pratikvyas1991/NetworkingExample/tree/master/app
这篇关于在 android 上调用 POST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!