2015-06-20 64 views
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尋求幫助。

回答

0

難道這是你在這個構造函數賦值爲null值的問題:

public DeptTable() { 
    super(); 
    this.no=0; 
    this.name = null; 
} 
+0

嘗試,現在,似乎什麼都沒有改變,我仍然得到這個錯誤。無論如何 –