使用retrofit框架
2016-08-11 17:09
169 查看
普通get请求
@GET("service/getIpInfo.php")
Call<String> getIpInfo(@Query("ip") String ip);
参数在body内的post请求
@Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
@POST("omcApp-web/order/queryOrderList.do?sign=b5f56819de1316305d29d848d89d14e0")
Call<String> getResponseByPostBody(@Body RequestBody route);
参数以键值对的post请求
@POST("mobileLogin")
Call<String> getResponseByPostMaps(@QueryMap Map<String,Object> map);
图片上传
@Multipart
@POST("medical-API/uploadFile/")
Call<String> getUploadImage(@Part("file\"; filename=\"image.jpg")RequestBody imgs);
WebService请求
@Headers({"Content-Type: text/xml","SOAPAction: url"})
@POST("SMS_Service.asmx?op=method")
Call<String> getWeatherbyCityName(@Body String requestEnvelope);
//====================以上是关于网络请求注解的api==================================
Map<String, Object> map = new HashMap<>();
map.put("params","params");
ApiService apiService = initNetWorkConfig().create(ApiService.class);
Call<String> call = apiService.getIpInfo("192.xx.xx.xx");
//postbody
RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
//post图片
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//postxml(webServices)
RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), xmlParams);
//map转xml
public String getMapToXml(Map<String,String> map,String methName){
String head = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
String ends = "</soap:Envelope>";
StringBuffer stringBuffer = new StringBuffer(head);
stringBuffer.append("<soap:Body>\n");
stringBuffer.append("<" + methName + " xmlns=\"http://tempuri.org/\">\n");
for (Map.Entry<String, String> entry : map.entrySet()) {
stringBuffer.append("<" + entry.getKey() + ">" + entry.getValue() + "</" + entry.getKey() + ">\n");
}
stringBuffer.append("</" + methName + ">\n");
stringBuffer.append("</soap:Body>\n");
stringBuffer.append(ends);
return stringBuffer.toString();
}
====================================以上是传参=====================================
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//成功回执
}
@Override
public void onFailure(Call<String> call, Throwable t) {
//失败回执
}
});
@GET("service/getIpInfo.php")
Call<String> getIpInfo(@Query("ip") String ip);
参数在body内的post请求
@Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
@POST("omcApp-web/order/queryOrderList.do?sign=b5f56819de1316305d29d848d89d14e0")
Call<String> getResponseByPostBody(@Body RequestBody route);
参数以键值对的post请求
@POST("mobileLogin")
Call<String> getResponseByPostMaps(@QueryMap Map<String,Object> map);
图片上传
@Multipart
@POST("medical-API/uploadFile/")
Call<String> getUploadImage(@Part("file\"; filename=\"image.jpg")RequestBody imgs);
WebService请求
@Headers({"Content-Type: text/xml","SOAPAction: url"})
@POST("SMS_Service.asmx?op=method")
Call<String> getWeatherbyCityName(@Body String requestEnvelope);
//====================以上是关于网络请求注解的api==================================
Map<String, Object> map = new HashMap<>();
map.put("params","params");
ApiService apiService = initNetWorkConfig().create(ApiService.class);
Call<String> call = apiService.getIpInfo("192.xx.xx.xx");
//postbody
RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
//post图片
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
//postxml(webServices)
RequestBody body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), xmlParams);
//map转xml
public String getMapToXml(Map<String,String> map,String methName){
String head = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
String ends = "</soap:Envelope>";
StringBuffer stringBuffer = new StringBuffer(head);
stringBuffer.append("<soap:Body>\n");
stringBuffer.append("<" + methName + " xmlns=\"http://tempuri.org/\">\n");
for (Map.Entry<String, String> entry : map.entrySet()) {
stringBuffer.append("<" + entry.getKey() + ">" + entry.getValue() + "</" + entry.getKey() + ">\n");
}
stringBuffer.append("</" + methName + ">\n");
stringBuffer.append("</soap:Body>\n");
stringBuffer.append(ends);
return stringBuffer.toString();
}
====================================以上是传参=====================================
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//成功回执
}
@Override
public void onFailure(Call<String> call, Throwable t) {
//失败回执
}
});
相关文章推荐
- 使用retrofit框架的网络请求方法使用例子,同时+MVP +rxjava + rxandroid使用
- 使用retrofit框架的网络请求方法使用例子,同时+MVP +rxjava + rxandroid使用
- Android学习和使用Retrofit框架
- 使用组件来保护你的ASP代码
- IP 伪装简易使用说明
- 源码推荐:一个使用C#绘制图形引擎的Framework
- Win32汇编教程六 工具栏和状态栏的使用
- Win32汇编教程五 菜单和加速键的使用
- Win32汇编教程三 一个简单的对话框 --- 兼谈资源文件的使用
- 使用ActiveX控件开发网页常见的问题
- 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图 (2)
- 使用 ASP+ DataGrid 控件来创建主视图/详细资料视图
- 使用ASP和Word进行服务器端拼写检查
- 单独使用CRecordSet
- 在 Web 页上使用条件数值格式
- 关于PHP与Sybase数据库的连接与使用
- [导入]Guru of the Week:#41 使用标准库
- 如何使用ASP进行打印操作
- VB中Winsock控制的UDP协议的使用
- FUNCky 的使用