2016-05-19 114 views
-1

我有這段代碼,我用它在本地主機上顯示一個listView,當我上傳它時,沒有任何顯示在我的ListView中。所以這就是我所做的。通常我會將所有數據保存在我的數據庫中,但屏幕是空的。Android使用JSON錯誤:「解析數據時出錯org.json.JSONException:值」

private ListView listView; 
public static final String URL="http://gabes.comlu.com/Base_Controle/getAllEmp.php"; 
// static String TAG_JSON_ARRAY=""; 
private String JSON_STRING; 
static JSONObject result = null; 
public static final String TAG_JSON_ARRAY = "result"; 

    private void showEmployee(){ 
     JSONObject jsonObject = null; 
      ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); 
      try { 
       jsonObject = new JSONObject(JSON_STRING); 
       JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY); 
      for(int i = 0; i<result.length(); i++){ 
       JSONObject jo = result.getJSONObject(i); 
       String nom1 = jo.getString("nom"); 
       String tele1 = jo.getString("tele"); 
       String grade1 = jo.getString("grade"); 
       String image1 = jo.getString("image"); 
       String site1 = jo.getString("site"); 
       String email1 = jo.getString("email"); 
       HashMap<String,String> employees = new HashMap<>(); 
       // employees.put("Type",type); 
       employees.put("log",nom1); 
       employees.put("a",tele1); 
       employees.put("pre",grade1); 
       employees.put("d",image1); 
       employees.put("c",site1); 
       employees.put("b", email1); 
       list.add(employees); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       getActivity(), list, R.layout.list_item1, 
       new String[]{"log","pre","a","b","c","d"}, 
       new int[]{R.id.id, R.id.name,R.id.a}); 

     listView.setAdapter(adapter); 
    } 
+0

發表您的logcat和json – Mohit

+0

請張貼你的json和你的stacktrace! –

+0

你沒有從網絡上獲得JSON? –

回答

1

你JSON_ARRAY常數沒有值:

static String TAG_JSON_ARRAY=""; 

您正試圖獲得一個空標籤的JSONArray,這就是爲什麼你會得到一個Exception

0

你可以使用GSON庫來解析這個json數據。

Gson gson = new Gson(); 
    Type listType = new TypeToken<ArrayList<test>>() { 
    }.getType(); 
    ArrayList<test> Data = (ArrayList<test>) gson.fromJson(json, listType); 

這裏測試類的JSON數據:

public class test { 

String nom; 
String add; 
String tele; 


public String getNom() { 
    return nom; 
} 

public void setNom(String nom) { 
    this.nom = nom; 
} 

public String getAdd() { 
    return add; 
} 

public void setAdd(String add) { 
    this.add = add; 
} 

public String getTele() { 
    return tele; 
} 

public void setTele(String tele) { 
    this.tele = tele; 
} 

}

0

謝謝大家:) 我通過編輯解決了這個問題:

private String JSON_STRING; 
static JSONObject result = null; 
...... 
try { 
       JSONArray result = new JSONArray(json); 
      for(int i = 0; i<result.length(); i++){ 
} 
protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 

       JSON_STRING = s; 
       showEmployee(s); 
      } 
相關問題