1

I am not very familiar with Android app Development.

In my previous app, I had used SOAP to connect android app with SQL Server using Asp.NET webservice. But then, I came to know that JSON would be the best method because of its parsing capabilities and many other advantages. I found many samples of code but they were all using PHP, whereas I am using asp.net.

Can anyone please help me to solve the problem?

Asp.NET Webservice Code :

[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 App Code:

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

I am getting Error Value html of type java.lang.String cannot be converted to JSONObject

Jeeten Parmar
  • 13
  • 1
  • 6

1 Answers1

0

you need to using asp.net web services and converting your data in json format and from android code you will request the service data

try this example http://osmosee.wordpress.com/2013/07/20/calling-a-json-based-asp-net-web-service-from-an-android-phone/

mohammed momn
  • 3,230
  • 1
  • 20
  • 16
  • @mohammed momn http://stackoverflow.com/questions/21979473/how-to-access-microsoft-sql-server-database-for-android-application i want the result from webservice as json from the .net websevice..how i get the value and parse it? can have any clue? – Noufal Feb 26 '14 at 06:48