androidはwebserviceデータを取得します。

2315 ワード

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