2
我是新来的webservice和JSON,只是说。 我使用ASP.NET web API连接SQL Server和我的android应用程序,但是当我到达解析点时,出现“没有值的值”错误,应用程序崩溃。JSON解析器没有值的JSONArray值
这里是在web服务查询的代码,它返回到服务器
public DataTable Tagmap()
{
DataTable deptTable = new DataTable();
deptTable.Columns.Add("no", typeof(String));
deptTable.Columns.Add("name", typeof(String));
if (dbConnection.State.ToString() == "Closed")
{
dbConnection.Open();
}
string query = "SELECT COUNT(R_NOME), R_NOME FROM RUBRICHEUTENTI GROUP BY R_NOME";
SqlCommand command = new SqlCommand(query, dbConnection);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
deptTable.Rows.Add(reader["no"], reader["name"]);
}
}
reader.Close();
dbConnection.Close();
return deptTable;
}
这里充满元素的DataTable的DataTable的代码在Android应用程式
public class DeptTable {
int no;
String name;
public DeptTable(int no, String name) {
super();
this.no = no;
this.name = name;
}
public DeptTable() {
super();
this.no=0;
this.name = null;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
这是调用解析方法的活动
public class DeptActivity extends Activity {
ArrayAdapter<String> adapter;
ListView listv;
Context context;
ArrayList<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dept);
// Show the Up button in the action bar.
//setupActionBar();
data = new ArrayList<String>();
listv = (ListView) findViewById(R.id.lv_dept);
context = this;
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
listv.setAdapter(adapter);
Toast.makeText(this,"Attendi...",Toast.LENGTH_SHORT).show();
new AsyncLoadDeptDetails().execute();
}
protected class AsyncLoadDeptDetails extends
AsyncTask<Void, JSONObject, ArrayList<DeptTable>> {
ArrayList<DeptTable> deptTable = null;
@Override
protected ArrayList<DeptTable> doInBackground(Void... params) {
// TODO Auto-generated method stub
RestAPI api = new RestAPI();
try {
JSONObject jsonObj = api.Tagmap();
JSONParser parser = new JSONParser();
deptTable = parser.parseDepartment(jsonObj);
} catch (Exception e) {
// TODO Auto-generated catch block
//Log.d("AsyncLoadDeptDetails", e.getMessage());
e.printStackTrace();
}
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();
}
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dept, menu);
return true;
}
最后,解析方法本身,我得到的错误在JSONArray jsonArray = object.getJSONArray(“Value”);然后在OnPostExecute
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("no"), jsonObj.getString("name")));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
//Log.d("JSONParser => parseDep", e.getMessage());
e.printStackTrace();
}
return arrayList;
}
级联我不知道要改变什么,我试图把在arrayList.add的optJSONArray或optInt/optString,但我得到一个NullPointerException异常。我只是一个初学者,所以我不太明白哪里可能是问题。
Thx寻求帮助。
尝试,现在,似乎什么都没有改变,我仍然得到这个错误。无论如何 –