简体   繁体   中英

Retrieve data from remote sqlserver to android app

I want to display data from my remote sql-server in my android app. I am using web-service. I am able to connect and to insert but not to display with JSON.

This is the code from web-service

 public DataTable RequestDetails(string request_name)
    {
        DataTable requestDetails = new DataTable();
        requestDetails.Columns.Add(new DataColumn("Request ID", typeof(String)));
        requestDetails.Columns.Add(new DataColumn("Date", typeof(String)));

        if(dbConnection.State.ToString() == "Closed")
        {
            dbConnection.Open();
        }

        string query = "select ID_Requests,request_date from Requests where request_by='" + request_name + "'";

        SqlCommand command = new SqlCommand(query, dbConnection);
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                requestDetails.Rows.Add(reader["Request ID"], reader["Date"]);
            }
        }

        reader.Close();
        dbConnection.Close();
        return requestDetails;
    }

This is the android code:

 protected class AsyncLoadDeptDetails extends
        AsyncTask<DeptTable, JSONObject, ArrayList<DeptTable>> {
    ArrayList<DeptTable> deptTable = null;

    @Override
    protected ArrayList<DeptTable> doInBackground(DeptTable... params) {
        // TODO Auto-generated method stub

        RestAPI api = new RestAPI();
       try {

            JSONObject jsonObj = api.RequestDetails(params[1].getName());

            JSONParser parser = new JSONParser();

            deptTable = parser.parseDepartment(jsonObj);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("AsyncLoadDeptDetails", e.getMessage());

        }

        return deptTable;
    }

    @Override
    protected void onPostExecute(ArrayList<DeptTable> result) {
        // TODO Auto-generated method stub

        for (int i = 0; i < result.size(); i++) {
            data.add(result.get(i).getNo() + " " + result.get(i).getName());
        }

        adapter.notifyDataSetChanged();

        Toast.makeText(context, "Loading Completed", Toast.LENGTH_SHORT).show();
    }

}

And the JSONParser code:

public ArrayList<DeptTable> parseDepartment(JSONObject object)
{
    ArrayList<DeptTable> arrayList=new ArrayList<DeptTable>();
    try {
        JSONArray jsonArray=object.getJSONArray("Value");
        JSONObject jsonObj=null;
        for(int i=0;i<jsonArray.length();i++)
        {
            jsonObj=jsonArray.getJSONObject(i);
            arrayList.add(new DeptTable(jsonObj.getInt("Request ID"), jsonObj.getString("Date")));
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        Log.d("JSONParser => parseDepartment", e.getMessage());
    }
    return arrayList;
}

I would use Fiddler or something similar and have a look at the data being returned. I've always had trouble returning a .NET Datatable from a service, so if it were me I would convert the datatable to JSON before sending. This has been discussed on several posts before, but here's a great answer: https://stackoverflow.com/a/17398078/3299157

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM