android 取得webservice数据

try{
  String SERVER_URL = "http://192.168.10.33:81/MobileService/MSServiceQuery.asmx/HPReadQueryPhone";
   HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求   
         request.addHeader("Content-Type", "application/json; charset=utf-8");//必须要添加该Http头才能调用WebMethod时返回JSON数据            
         JSONObject jsonParams=new JSONObject();
//         jsonParams.put("strdate", "1");
//         jsonParams.put("aa", "1");
         jsonParams.put("strSql", "select * from RRUser");//传参,如果想传递两个参数则继续添加第二个参数jsonParams.put("param2Name","param2Value")
         jsonParams.put("BD", "27");
         HttpEntity bodyEntity =new StringEntity(jsonParams.toString(), "utf8");//参数必须也得是JSON数据格式的字符串才能传递到服务器端,否则会出现"{'Message':'strUserName是无效的JSON基元'}"的错误
         Log.i("ex",jsonParams.toString());
         request.setEntity(bodyEntity);
         HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈
         ShowMessage(httpResponse.getStatusLine().toString());
         if(httpResponse.getStatusLine().getStatusCode()==200)
         {
         
         String result = EntityUtils.toString(httpResponse.getEntity());
         Log.i("ex", result);
         ShowMessage(result);
         if(!result.equals(""))
         {
          try
          {
           JSONArray   jsonObjs = new JSONObject (result).getJSONArray("Table");  
           JSONObject jsonObj = ((JSONObject)jsonObjs.opt(0));
               String username=jsonObj.getString("UserCname");
               ShowMessage(username);
          Log.i("ex",String.valueOf(jsonObjs.length())+"aa");
          }
          catch(JSONException e)
          {
           ShowMessage("数据错误");
          }
         }
      
         }
         else
         {
          ShowMessage("网络传输错误");
         }
  }
  catch(Exception e)
  {
   Log.i("ex", e.getMessage());
  }

你可能感兴趣的:(android 取得webservice数据)