Android开发 Json服务端调用
来源:互联网 发布:seo职业发展方向 编辑:程序博客网 时间:2024/06/10 08:32
最近做Android的开发用到了webservice,整理如下,此方法需要在新线程里面调用
android端:
public void loginSupermarket(String url,String userPhoneString,String userPasswordString) {
try {HttpClient client = new DefaultHttpClient();
HttpPost postjson = new HttpPost(url);//url为服务端的url
ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("selectMethod", "login"));//向服务端发送的参数
nvps.add(new BasicNameValuePair("userPhoneString",userPhoneString ));
nvps.add(new BasicNameValuePair("userPasswordString", userPasswordString));
UrlEncodedFormEntity entityIn = new UrlEncodedFormEntity(nvps,"gbk");//发送的编码格式
postjson.setEntity(entityIn);
HttpResponse response = client.execute(postjson);
HttpEntity entityOut = response.getEntity();//获得返回来的json实体,下面是解析
System.out.println(2);
if (entityOut != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(entityOut.getContent(), "gbk"));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
JSONTokener jsonTokener=new JSONTokener(sb.toString());
JSONObject jsonObject=(JSONObject)jsonTokener.nextValue();
if(jsonObject.getBoolean("loginResult"))//下面是跟据服务端返回来的值向子线程发送数据
{
Message msg = new Message();
Bundle b = new Bundle();// 存放数据
b.putBoolean("loginResult", true);
msg.setData(b);
Login.this.loginHandler.sendMessage(msg); // 向Handler发送消息,更新UI
}
else {
Message msg = new Message();
Bundle b = new Bundle();// 存放数据
//b.putString("result", "我的");
b.putBoolean("loginResult", false);
msg.setData(b);
Login.this.loginHandler.sendMessage(msg); // 向Handler发送消息,更新UI
}
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
服务端//这段在web端的servlet里面
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/json;charset=UTF8");
request.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
String selectMethod=request.getParameter("selectMethod");
if(selectMethod.equalsIgnoreCase("login"))
{
String userPhoneString=request.getParameter("userPhoneString");
String userPasswordString=request.getParameter("userPasswordString");
try {
System.out.println(userPhoneString);
System.out.println(userPasswordString);
custom custom1=operateDataForApi.login(userPhoneString, userPasswordString);
if(custom1!=null)
{
System.out.println("true");
jobj.put("loginResult",true);
}else
{
System.out.println("false");
jobj.put("loginResult", false);
}
out.print(jobj.toString());
}
catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
}
- Android开发 Json服务端调用
- json作为客户端调用服务端说明
- BOS开发 服务端调用BOTP
- android调用服务端的js
- 【Android 开发】: Android客户端与服务端之间使用JSON交互数据。
- Android客户端与JAVA服务端JSON传值
- Android与服务端通信之JSON格式
- android发送json数据到服务端
- android客户端通过json连接servlet服务端
- HTTP服务端JSON服务端
- Android流媒体开发-服务端搭建
- Android开发-Swiftp服务端移植
- SpringMvc 服务端发布json数据+android客户端解析json数据
- Android使用Hessian远程调用WebService服务端
- 开发android服务端的开发环境配置
- Android服务端开发1-使用Eclipse搭建Java Web服务端
- Android服务端开发1--使用Eclipse+Tomcat搭建Web服务端
- Android服务端开发-使用Eclipse搭建Java Web服务端
- JavaWeb开发中,文件下载的时候,ie和firefox文件名的处理
- jquery easyui show dialog 的使用
- 摘要
- Apple's OpenGL——Vertex Shader基础
- 迭代器失效
- Android开发 Json服务端调用
- CXF SOAP协议修改为1.2
- Matlab 之图像处理相关函数
- Dropping tests - POJ 2976 二分
- 关于函数strtok和strtok_r的使用要点和实现原理
- Linux压缩和解压缩命令大全
- source insight工程的相对路径
- weblogic上session失效问题
- 关注博主的博文汇总(孙志刚、贺利坚……)