android中的Listview没有填充数据

时间:2014-09-03 20:51:02

标签: java android json listview import

这是我在c#framework 4.5中的webservice代码,用于检索数据并将其添加到json数组中。

[WebMethod(Description = "Webservice for generating category wise report in JSON form")]
public string getCategoryWiseReport(string distr)
{
    string dist = "23";
    distr = dist;
    var con = new SqlConnection("data source=xxxx;initial catalog=xxxxx;integrated security=true");
    StringBuilder JSON = new StringBuilder();
    con.Open();
    var cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "RepSum";
    cmd.CommandType = CommandType.StoredProcedure;
    DataTable dt = new DataTable();
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    cmd.Parameters.AddWithValue("@dri", dri);
    cmd.ExecuteNonQuery();
    ad.Fill(dt);


    JSON.Append("{");
    JSON.Append("\"Report\":[");
    for (int i = 0; i <= (dt.Rows.Count - 1); i++)
    {
        for (int j = 0; j <= 8; j++)
        {
            JSON.Append("{");
            JSON.Append("\"code\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"desg\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"name\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"name\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"complaint_re\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"omplaint_d\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"complaint_pg\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"complaint_im\":\"" + dt.Rows[i][j].ToString() + "\", ");
            JSON.Append("\"omplaint_t\":\"" + dt.Rows[i][j].ToString() + "\", ");
        }

        if (JSON.ToString().EndsWith(","))
        {
            JSON = JSON.Remove(JSON.Length - 1, 1);
        }

        JSON.Append("]}");
    }
    con.Close();
    return JSON.ToString();

}

这是我的Android应用程序,当我执行这个应用程序时,它的工作基本上用来自上面的webservice的json数据填充listview。错误是

   ::: org.json.jsonexception: expected literal value at character 211 of {"Report" :[{

    package com.example.newasynctask;

 import java.util.ArrayList;
 import java.util.HashMap;

 import org.json.JSONArray;
 import org.json.JSONObject;
 import org.ksoap2.SoapEnvelope;
 import org.ksoap2.serialization.PropertyInfo;
 import org.ksoap2.serialization.SoapObject;
 import org.ksoap2.serialization.SoapPrimitive;
  import org.ksoap2.serialization.SoapSerializationEnvelope;
  import org.ksoap2.transport.HttpTransportSE;

 import android.os.AsyncTask;
  import android.os.Bundle;
   import android.app.Activity;
 import android.app.ListActivity;
  import android.app.ProgressDialog;
  import android.view.Menu;
 import android.widget.ListAdapter;
   import android.widget.ListView;
   import android.widget.SimpleAdapter;

  public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

private static final String NAMESPACE="http://tempuri.org/";
private static final String URL="http://10.0.2.2:2464/WebServiceInsUpd/WebService.asmx";
private static final String SOAP_ACTION="http://tempuri.org/getCategoryWiseReport ";
private static final String METHOD_NAME="getCategoryWiseReport";
private static final String Report= "Report";                                      
private static final String code= "code";
private static final String desg = "desg ";
private static final String name = "name ";
private static final String name = "name ";
private static final String total_comp = "total_comp ";
private static final String pg = "pg ";
private static final String ded = "ded ";
private static final String im = "inim ";
private static final String defaulter = "defaulter ";
ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new getContacts().execute();
}

private class getContacts extends AsyncTask<Void, Void, Void>{


    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  


          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);
         //enve

          HttpTransportSE ht = new HttpTransportSE(URL);

          try{
          ht.call(SOAP_ACTION, envelope);
          SoapPrimitive response;
          response = (SoapPrimitive)envelope.getResponse();
            final String JSON= response.toString();
            JSONObject jsonObj = new JSONObject(JSON);

            // Getting JSON Array node
           JSONArray contacts = jsonObj.getJSONArray(Report);

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                // tmp hashmap for single contact
                HashMap<String, String> contactdata = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contactdata.put(code, c.getString(ofc_code));
                contactdata.put(desg , c.getString(ofc_desg));
                contactdata.put(name , c.getString(ofc_name));
                contactdata.put(name , c.getString(dep_name));
                contactdata.put(comp , c.getString(total_comp));
                contactdata.put(pg , c.getString(pending));
                contactdata.put(dsed , c.getString(desposed));
                contactdata.put(iim, c.getString(interim));
                contactdata.put(defer , c.getString(defaulter ));
                // adding contact to contact list
               contactList.add(contactdata);
            }
          }
            catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        return null;
    }



    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListView lw=(ListView)findViewById(R.id.lst);
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,R.layout.list_xml, new String[] { code,desg,name }, new int[] { R.id.name,R.id.email, R.id.mobile });
        setListAdapter(adapter);

    }

}
}

activity_main.xml中

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="vertical"
>
<ListView 
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@android:id/list"
></ListView>


 </LinearLayout>

list_xml

    <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
     android:padding="10dip"
     android:paddingLeft="10dip"
     android:paddingRight="10dip"
     >
     <TextView
         android:id="@+id/name"
          android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingBottom="2dip"
         android:paddingTop="6dip"
         android:textColor="#43bd00"
         android:textSize="16sp"
          android:textStyle="bold" />

      <!-- Email label -->
      <TextView
         android:id="@+id/email"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:paddingBottom="2dip"
          android:textColor="#acacac" />

       <!-- Mobile number label -->
       <TextView
          android:id="@+id/mobile"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="left"
          android:text="Mobile: "
           android:textColor="#5d5d5d"
            android:textStyle="bold" />

    </LinearLayout>

0 个答案:

没有答案