2016-05-19 8 views
-1

ローカルホストでこのコードを使用してアップロードしたときにlistViewを表示しましたが、ListViewに何も表示されません。それで、私は何をしたのですか?通常、私はすべてのデータをデータベースに保存しますが、画面は空です。JSONエラーを使用しているAndroid:「データの解析中にエラーが発生しました。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とスタックトレースを投稿してください! –

+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); 
      } 
関連する問題