Android开发 Json服务端调用

最近做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();
     
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值