Retrofit进行post提交json数据
1:先看一看xutils3的提交代码
String account = editText1.getText().toString();
String password = editText2.getText().toString();
JSONObject js_request = new JSONObject();//服务器需要传参的json对象
try {
js_request.put("account", account);//添加相应键值对
js_request.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
RequestParams requestParams = new RequestParams(LOGIN_URL);
requestParams.setAsJsonContent(true);
requestParams.setBodyContent(js_request.toString());
x.http().post(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
System.out.println("**ok"+result);
try {
JSONObject object = new JSONObject(result);
String code = object.getString("code");
if (code.equals("1")) {
// button.setClickable(false);
//登录成功后获得id } else {
// 登陆失败
} } catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
System.out.println("errot");
} @Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
2:Retrofit提交过程
2.1 登陆 urL
public static String LOGIN_URL = "http://114.xx.xxx.xx:8088/vdyweb/ws/rest/Login";
interface APIStore {
@Headers({"Content-Type: application/json","Accept: application/json"})//需要添加头
@POST ("vdyweb/ws/rest/Login")
Call<ResponseBody>getMessage(@Body RequestBody info); // 请求体味RequestBody 类型
}
public class Info {
String account;
String password;
public Info(String account, String password) {
this.account = account;
this.password = password;
}
}
public class MainActivity extends AppCompatActivity {
public static String BASE_LOGIN_URL = "http://114.xx.xxx.xx:8088/";
Retrofit retrofit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Info info=new Info("test","123456");
/*** 利用Gson 将对象转json字符串*/
Gson gson=new Gson();
String obj=gson.toJson(info);
retrofit=new Retrofit.Builder().baseUrl(BASE_LOGIN_URL).build();
RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),obj);
final APIStore login = retrofit.create(APIStore.class);
retrofit2.Call<ResponseBody> data = login.getMessage(body);
data.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "onResponse: --ok--"+response.body());
try {
Log.d(TAG, "onResponse: --ok--"+response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "onResponse: --err--"+t.toString());
} });
} }
3:添加get请求
apiStore加
@GET("vdyweb/ws/rest/device/getOwnerDevice/2/2/20")
Call<ResponseBody>getMessage2();
retrofit2.Call<ResponseBody>data1=login.getMessage2();
data1.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.d(TAG, "onResponse: --ok--"+response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) { }
});
Retrofit进行post提交json数据的更多相关文章
- ngResource提交json数据如何带参数
ngResource提交json数据如何带参数 直接使用ngResource和REST服务接口交互可以让程序显得简洁,前提是配置好跨域和OPTIONS请求的支持,与此同时,如果需要带些额外的参数,有两 ...
- Jquery Ajax 提交json数据
在MVC控制器(这里是TestController)下有一个CreateOrder的Action方法 [HttpPost] public ActionResult CreateOrder(List&l ...
- Jquery-ajax()方法提交json数据
1.ajax()提交json数据代码 var strJson = getStrPayJson(); $.ajax({ type: "POST", url: "/userc ...
- ajax提交json数据到后端C#解析
本文链接:https://blog.csdn.net/qq_22103321/article/details/78015920 前端提交json数据 $.ajax({ type: "post ...
- 前端ajax用post方式提交json数据给后端时,网络报错 415
项目框架:spring+springmvc+mybatis 问题描述:前端ajax用post方式提交json数据给后端时,网络报错 415 前端异常信息:Failed to load resource ...
- 导出excel时,以form方式提交json数据
今天在写项目时写到一个excel的导出,开始想用ajax请求后台后导出,但发现ajax会有返回值,而且ajax无法直接输出文件,而后台的excel导出方法已经封装好,不方便修改. 就改用了提交的方式f ...
- jQuery提交Json数据到Webservice,并接收返回的Json数据
jQuery ajax webservice:get 和 post 一.GET 方式 客户端 复制代码 代码如下: var data = { classCode: "0001"}; ...
- 关于ajax 进行post提交 json数据到controller
首选需要参考的两个博客: www.cnblogs.com/Benjamin/archive/2013/09/11/3314576.html http://www.cnblogs.com/quanyon ...
- iOS通过ASIHTTPRequest提交JSON数据
先验知识——什么是ASIHTTPRequest? 使用iOS SDK中的HTTP网络请求API,相当的复杂,调用很繁琐,ASIHTTPRequest就是一个对CFNetwork API进行了封装,并且 ...
随机推荐
- Python中字典的相关操作
1. Python类似于Java中的哈希表,只是两种语言表示的方式是不一样的,Python中的字典定义如下: 在Python中是一种可变的容器模型,它是通过一组键(key)值(value)对组成,这种 ...
- 数据结构总结(UPDATING......)
目标: 1.栈........√ 2.队列......√ 3.堆.........× 4.并查集...× 栈: #define MAXN 65536 struct stack{ int sz[MAXN ...
- Python学习笔记之函数
这篇文章介绍有关 Python 函数中一些常被大家忽略的知识点,帮助大家更全面的掌握 Python 中函数的使用技巧 1.函数文档 给函数添加注释,可以在 def 语句后面添加独立字符串,这样的注释被 ...
- 《奋斗吧!菜鸟》 第八次作业:Alpha冲刺 Scrum meeting 3
项目 内容 这个作业属于哪个课程 任课教师链接 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11012922.html 团队名称 奋斗吧!菜鸟 作业学习目标 A ...
- c# 数组 字符串 C#中判断字符串中包含某个字符
string str = "1,2,3,4,5,6,7"; string[] strArray = str.Split(','); //字符串转数组 ...
- UVa - 11283 - PLAYING BOGGLE
先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...
- POJ 1987
T_T为毛会这样子,我的写就是过不了,....... 其实这题不难,很容易想到吧,我一开始也想着用枚举这类方法,但复杂度实在不敢想,没想到,真的是用这种方法.. 今天学了一个叫树的重心,可以使分治的子 ...
- Thread.suspend和println使线程死锁
Thread.suspend和println使线程死锁 package com.stono.thread2.page39; public class MyThread extends Thread{ ...
- [React] Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3
The componentWillReceiveProps() method is being deprecated in future version of React (17). Many of ...
- GNUPlot绘制曲线
发现gnuplot在mac上编译安装相当方便,在线下为了測试java老堆和lucene索引大小,须要绘制两条线,可是直接点连的线很难看,所以后面使用贝塞尔曲线. 脚本例如以下: #! /usr/loc ...