2014-01-21 52 views
1

我不是很熟悉Android app DevelopmentAndroid JSON解析使用Asp.NET JSON webservice和SQL Server

在我以前的應用程序中,我使用SOAP使用Asp.NET web服務將android應用程序與SQL Server連接起來。但後來,我開始知道JSON是最好的方法,因爲它的解析能力和其他許多優點。我發現了很多代碼示例,但它們都使用了PHP,而我使用的是asp.net

任何人都可以請幫我解決這個問題嗎?

Asp.NET Webservice的代碼:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string GetCity() 
    { 
     DAL dal = new DAL(); 

     try 
     { 
      List<City> lstCity = new List<City>(); 

      DataTable dt = dal.GetDataTable("Select CityId, CityName from tblCity", "TEXT"); 

      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       City and = new City(); 
       and.CityId = Convert.ToInt64(dt.Rows[i]["CityId"].ToString()); 
       and.CityName = dt.Rows[i]["CityName"].ToString(); 
       lstCity.Insert(i, and); 
      } 

      JavaScriptSerializer jsCity = new JavaScriptSerializer(); 
      return jsCity.Serialize(lstCity); 
     } 
     catch (Exception ex) 
     { 
      return ex.Message.ToString(); 
     } 
    } 

Android應用程序代碼:

private void populateSpinner() { 
     List<String> lables = new ArrayList<String>(); 

     tv.setText(""); 

     for (int i = 0; i < cityList.size(); i++) { 
      lables.add(cityList.get(i).getName()); 
     } 

     ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, lables); 

     spinnerAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     spinnerFood.setAdapter(spinnerAdapter); 
    } 

    private class GetCities extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(CityData.this); 
      pDialog.setMessage("Fetching Cities.."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     @Override 
     protected String doInBackground(String... arg0) { 
      ServiceHandler sh = new ServiceHandler(); 

      String json = sh 
        .makeServiceCall(URL_CATEGORIES, ServiceHandler.GET); 

      Log.e("Response: ", "> " + json); 

      if (json != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(json); 
        if (jsonObj != null) { 
         JSONArray cities = jsonObj.getJSONArray("GetCity"); 

         for (int i = 0; i < cities.length(); i++) { 
          JSONObject catObj = (JSONObject) cities.get(i); 
          City cat = new City(catObj.getInt("CityId"), 
            catObj.getString("CityName")); 
          cityList.add(cat); 
         } 
        } 

       } catch (JSONException e) { 
        // e.printStackTrace(); 
        result = e.getMessage().toString(); 
       } 

      } else { 
       Log.e("JSON Data", "Didn't receive any data from server!"); 
      } 

      return result; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      populateSpinner(); 

      tv.setText(result); 
     } 
    } 

java.lang.String類型的我得到錯誤值HTML不能轉換到JSONObject

+0

? – Noufal

回答