http传输json进行服务端接口与客户端对接,以及restful实现
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
话不多说,直接贴代码,照写就好了
服务端接口编写,我用的是springmvc,服务端接口其实和平时web开发一样,就是返回出json就好了,还有就是接受数据也是json,方法如下:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userservice;
@RequestMapping("/getUserByName")
public @ResponseBody User getUserByName(HttpServletRequest request) throws IOException{
StringBuffer str = new StringBuffer();
try {
BufferedInputStream in = new BufferedInputStream(request.getInputStream());
int i;
char c;
while ((i=in.read())!=-1) {
c=(char)i;
str.append(c);
}
}catch (Exception ex) {
ex.printStackTrace();
}
JSONObject obj= JSONObject.fromObject(str.toString());
System.out.println(obj.get("name"));
User user= userservice.getUserByName(obj.get("name").toString());
return user;
}
}
实现的是一个通过服务端接收客户端json{“name”:"cwh"}名字name进行查询操作,然后给客户端返回json;
客户端实现如下:
import java.io.IOException;
import net.sf.json.JSONObject;
import net.spring.model.User;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class Client {
@Test
public void HttpPostData() {
try {
HttpClient httpclient = new DefaultHttpClient();
String uri = "http://localhost:8080/springMVC/user/getUserByName";
HttpPost httppost = new HttpPost(uri);
//添加http头信息
httppost.addHeader("Authorization", "your token"); //认证token
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "imgfornote");
JSONObject obj = new JSONObject();
obj.put("name", "cwh");
httppost.setEntity(new StringEntity(obj.toString()));
HttpResponse response;
response = httpclient.execute(httppost);
//检验状态码,如果成功接收数据
int code = response.getStatusLine().getStatusCode();
System.out.println(code+"code");
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "","name": ""}
obj= JSONObject.fromObject(rev);
User user = (User)JSONObject.toBean(obj,User.class);
System.out.println("返回数据==="+user.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ok,把服务端部署到tomcat
运行客户端:返回数据成功
当然也可以用RESTFUL风格来实现
服务端可以这么编写:
@RequestMapping("/getUserByName/{name}")
public @ResponseBody User getUserByName(@PathVariable("name")String name) throws IOException{
User user= userservice.getUserByName(name);
return user;
}
那么客户端请求就这么写了:
public void HttpPostData() {
try {
HttpClient httpclient = new DefaultHttpClient();
String uri = "http://localhost:8080/springMVC/user/getUserByName/cwh";
HttpPost httppost = new HttpPost(uri);
JSONObject obj = new JSONObject();
HttpResponse response;
response = httpclient.execute(httppost);
//检验状态码,如果成功接收数据
int code = response.getStatusLine().getStatusCode();
System.out.println(code+"code");
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "","name": ""}
obj= JSONObject.fromObject(rev);
User user = (User)JSONObject.toBean(obj,User.class);
System.out.println("返回数据==="+user.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
这样也是可以的
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
3 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)