android org.json.JSONException

时间:2017-04-26 13:24:27

标签: android json

你好我的代码有错误告诉我org.json.JSONException在我尝试恢复打印机上的json的字符0输入结束时,通常会发生这样的错误,它返回一个空字符串,但我做不明白不是......

这是我的代码:

public class MaterielAssocie extends Activity {
private ListView lv;
public static etablissement etabSELECT;
private ProgressDialog progressDialog;
public static ArrayList<imprimante>detail = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.materiel_associe);
    lv = (ListView) findViewById(R.id.listViewMa);
    chargerJeuDessaie();
    rafraichirListView();
}

private void chargerJeuDessaie()
{
    progressDialog = progressDialog.show(this,"Gestion des services associés","Telechargement en cours");
    MonAsyncTaskHttp monAsyncTaskHttp = new MonAsyncTaskHttp() {

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            MaterielAssocie.detail.clear();
            try {
                JSONArray jsonArray = new JSONArray(o.toString());
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    imprimante imp = imprimante.factoryImp(jsonObject);
                    MaterielAssocie.detail.add(imp);

                    rafraichirListView();


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            progressDialog.dismiss();
        }
    };
    monAsyncTaskHttp.execute("requete=getLesImprimantes=" + search_ville.etabSELECT.getId());
}

public void rafraichirListView() {
    ArrayAdapter<imprimante> imprimanteadapter = new ArrayAdapter<imprimante>(MaterielAssocie.this, android.R.layout.simple_list_item_1, detail);
    lv.setAdapter(imprimanteadapter);
}

这是我的页面打印机的代码:

public class imprimante extends Activity {
    private int id_imprimante;
    private String marque_imprimante;
    private String emplacement;
    private int ref_etab;



 public imprimante(int id_imprimante, String marque_imprimante, String emplacement, int ref_etab) {
    this.id_imprimante = id_imprimante;
    this.marque_imprimante = marque_imprimante;
    this.emplacement = emplacement;
    this.ref_etab = ref_etab;
}

public int getId_imprimante() {
    return id_imprimante;
}

public void setId_imprimante(int id_imprimante) {
    this.id_imprimante = id_imprimante;
}

public String getMarque_imprimante() {
    return marque_imprimante;
}

public void setMarque_imprimante(String marque_imprimante) {
    this.marque_imprimante = marque_imprimante;
}

public String getEmplacement() {
    return emplacement;
}

public void setEmplacement(String emplacement) {
    this.emplacement = emplacement;
}

public int getRef_etab() {
    return ref_etab;
}

public void setRef_etab(int ref_etab) {
    this.ref_etab = ref_etab;
}
public String toString() {
    String lib = ""+ marque_imprimante +"  "+ emplacement +"";
    return lib;
}

public static imprimante factoryImp(JSONObject jo)
{
    imprimante imp = null;
    try {
        int id_imprimante = jo.getInt("id_imprimante");
        String marque_imprimante = jo.getString("marque_imprimante");
        String emplacement = jo.getString("emplacement");
        int ref_etab = jo.getInt("ref_etab");




        imp = new imprimante(id_imprimante,marque_imprimante,emplacement,ref_etab);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return imp;
}
}

如果有人看到错误我没有看到谢谢

错误:

     W/System.err: org.json.JSONException: End of input at character 0 of 
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:87)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:103)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at ddec.applicationddec.MaterielAssocie$1.onPostExecute(MaterielAssocie.java:43)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at 

android.os.AsyncTask.finish(AsyncTask.java:631)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-26 14:51:33.025 24113-24113/ddec.applicationddec W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)

JSON字符串

{
"type": "object",
"properties": {
    "id_imprimante": {
        "type": "int"
    },
    "marque_imprimante": {
        "type": "String"
    },
    "emplacement": {
        "type": "String"
    },
    "ref_etab": {
        "type": "int"
    },
  }
}

3 个答案:

答案 0 :(得分:1)

请把你的json字符串here并检查它是否有效

答案 1 :(得分:1)

您提供的JSON字符串无效,最后还有一个额外的逗号。

"ref_etab": { "type": "int" },应为"ref_etab": { "type": "int" }

答案 2 :(得分:1)

有一个逗号,请检查您的JSON格式   enter image description here