Java Android:Restful WS的结果未显示在我的ListView上

Java Android:Restful WS的结果未显示在我的ListView上,java,android,json,rest,listview,Java,Android,Json,Rest,Listview,我正在开发一个andeoid应用程序,它使用一个restfulweb服务从Mysql数据库检索数据。 所以我试图在我的ListView上显示webMethode的结果,但是列表总是空的。 结果是JSONformat,但为字符串 我制作了一个包含列表视图的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

我正在开发一个andeoid应用程序,它使用一个restfulweb服务从Mysql数据库检索数据。 所以我试图在我的ListView上显示webMethode的结果,但是列表总是空的。 结果是JSONformat,但为字符串

我制作了一个包含列表视图的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="La liste des DAB" />

    <ListView
        android:id="@+id/lstdab"
        style="@style/Widget.AppCompat.ListView.DropDown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible" />
</LinearLayout>
这是我的网络方法:

 // DAB zone Tunis
        @GET
        @Path("/RechercherDABzTunis")
        @Produces(MediaType.APPLICATION_JSON)
     //   @Consumes(MediaType.APPLICATION_JSON)
        public String DABtunis()   
               throws SQLException, JSONException {
            JSONObject obj = new JSONObject();
            JSONArray array=new JSONArray();


        dbCoN = new DBConnection();
        query = "SELECT dab.id_DAB, dab.libelle_DAB , etat.titre_etat , etat.libelle,zone.nom_zone FROM dab, agence,zone,etat where dab.id_agence=agence.id_agence and agence.id_zone=zone.id_zone and zone.nom_zone='Tunis' GROUP by dab.id_DAB;";

            try {
                conn = (Connection) DBConnection.createConnection();
                rslt = dbCoN.getResutlSet(query, conn);
                System.out.println("try here ! ! !  ");

                if (rslt.next()) {   
                while (rslt.next()){
                    obj = DAB.DABzoneToJson(rslt.getInt("id_DAB"),rslt.getString("libelle_DAB"),rslt.getString("titre_etat"),rslt.getString("libelle"),rslt.getString("nom_zone") );
                    array.put(obj.toString());


                    System.out.println(rslt.getString(1).toString());
                     }
                }
                else {
                    obj=DAB.DABToJsonFaux();
                    array.put(obj.toString());

                    System.out.println("");

                     }

                } catch (SQLException e){
                    System.out.println("exception here sql! ! "+e);
                } catch (Exception ex){
                    System.out.println("exception here ! ! "+ex);
                } finally { 
                    if (conn != null) {
                        conn.close();
                                    }
                }
            //JSONObject jobj=new JSONObject();
        //  jobj.put("array", array.toString());

            return array.toString();

        }
我认为问题出在这两个例外上,但我看不出有任何缺点。 这是我的webMethode o postman的结果:

[
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":22,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":26,\"etat\":\"En marche\",\"Libelle_DAB\":\"ooo\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":27,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":6660,\"etat\":\"En marche\",\"Libelle_DAB\":\"yyy\"}"
]
i'm using Android Studio , and wildfly 8 as a server, my web Service is a  r
estful我在同一个应用程序中有一个登录活动,它工作得很好,我认为问题出在解析上,但我没有看到错误,请解释一下好吗?谢谢大家!


更新:正如您所说,我添加了asyncTask,没有收到日志中Web服务的结果,但我仍然无法在ListView上显示它,我更新了活动代码,日志也更新了

您需要添加一个AsyncTask来运行api调用,否则网络将失败,因为它正在UI线程上运行。
查看一个例子。

看起来像是你在主线程上发出网络请求,这在android上是一个大禁忌

尝试在IntentService的新线程、异步任务或事件上执行此操作

一些阅读材料

http连接中出错android.os.NetworkOnMainThreadException

尝试使用AsyncTask从HTTP请求获取JSON。或者使用Volley库,因为它易于实现

分析数据org.json.JSONException时出错:输入字符0处结束 的

您在JSON_data.getStringid_DAB处收到JSON解析错误,因为id_DAB包含int值

试试这个:

更新:

E/ERROR:CODE:org.json.JSONException:Value中的错误 {地区:突尼斯,描述:le DAB fonctionne 更正,id:22,etat:En marche,诽谤:}0 无法将java.lang.String类型的转换为JSONObject

以下是工作代码:


希望这将有助于~

尝试使用截击检索数据谢谢!它可以工作,但我仍然抛出一个JSONException它是什么?在此发布您的try block use>>JSONArray jArray=newjsonarrayresult;而不是JSONArray jArray=新的JSONArray响应;我仍然有相同的错误,我认为它在JSONObject json_data=jArray.getJSONObject上;部分原因是当我试图在日志上显示它时,我找不到它,我已经用完全有效的代码更新了我的答案。请检查这个是的,谢谢!现在我可以在我的日志上看到结果,但我仍然无法在我的列表视图上显示它是的,谢谢,它可以与asynkTask一起工作!现在我可以在我的日志上看到结果,但是我仍然无法在我的ListView上显示它。在onPostExecute中这样做,但它已经在onPostExecute方法上了。我更新了关于这个问题的代码。
 04-24 20:51:29.355 20374-20636/com.example.projetmonitoringapplication E/test: ----["{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":22,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":26,\"etat\":\"En marche\",\"Libelle_DAB\":\"ooo\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":27,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}","{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":6660,\"etat\":\"En marche\",\"Libelle_DAB\":\"yyy\"}"]
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication E/ERROR: ERROR IN CODE: org.json.JSONException: Value {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":22,"etat":"En marche","Libelle_DAB":""} at 0 of type java.lang.String cannot be converted to JSONObject
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err: org.json.JSONException: Value {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":22,"etat":"En marche","Libelle_DAB":""} at 0 of type java.lang.String cannot be converted to JSONObject
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at org.json.JSONArray.getJSONObject(JSONArray.java:514)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at com.example.projetmonitoringapplication.listez$TheTask.onPostExecute(listez.java:141)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at com.example.projetmonitoringapplication.listez$TheTask.onPostExecute(listez.java:106)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:632)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.os.Looper.loop(Looper.java:136)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5021)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-24 20:51:29.365 20374-20374/com.example.projetmonitoringapplication W/System.err:     at dalvik.system.NativeStart.main(Native Method)
 // DAB zone Tunis
        @GET
        @Path("/RechercherDABzTunis")
        @Produces(MediaType.APPLICATION_JSON)
     //   @Consumes(MediaType.APPLICATION_JSON)
        public String DABtunis()   
               throws SQLException, JSONException {
            JSONObject obj = new JSONObject();
            JSONArray array=new JSONArray();


        dbCoN = new DBConnection();
        query = "SELECT dab.id_DAB, dab.libelle_DAB , etat.titre_etat , etat.libelle,zone.nom_zone FROM dab, agence,zone,etat where dab.id_agence=agence.id_agence and agence.id_zone=zone.id_zone and zone.nom_zone='Tunis' GROUP by dab.id_DAB;";

            try {
                conn = (Connection) DBConnection.createConnection();
                rslt = dbCoN.getResutlSet(query, conn);
                System.out.println("try here ! ! !  ");

                if (rslt.next()) {   
                while (rslt.next()){
                    obj = DAB.DABzoneToJson(rslt.getInt("id_DAB"),rslt.getString("libelle_DAB"),rslt.getString("titre_etat"),rslt.getString("libelle"),rslt.getString("nom_zone") );
                    array.put(obj.toString());


                    System.out.println(rslt.getString(1).toString());
                     }
                }
                else {
                    obj=DAB.DABToJsonFaux();
                    array.put(obj.toString());

                    System.out.println("");

                     }

                } catch (SQLException e){
                    System.out.println("exception here sql! ! "+e);
                } catch (Exception ex){
                    System.out.println("exception here ! ! "+ex);
                } finally { 
                    if (conn != null) {
                        conn.close();
                                    }
                }
            //JSONObject jobj=new JSONObject();
        //  jobj.put("array", array.toString());

            return array.toString();

        }
[
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":22,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":26,\"etat\":\"En marche\",\"Libelle_DAB\":\"ooo\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":27,\"etat\":\"En marche\",\"Libelle_DAB\":\"\"}",
  "{\"zone\":\"Tunis\",\"description\":\"le DAB fonctionne correctement.\",\"id_DAB\":6660,\"etat\":\"En marche\",\"Libelle_DAB\":\"yyy\"}"
]
i'm using Android Studio , and wildfly 8 as a server, my web Service is a  r
//create a new person
Dabl resultRow = new Dabl();

//set that person's attributes
resultRow.id = json_data.getInt("id_DAB"); 
resultRow.libdab = json_data.getString("Libelle_DAB");
resultRow.etat = json_data.getString("etat");
resultRow.des= json_data.getString("description");
resultRow.zone= json_data.getString("zone");
public void parseJson(){

    // Your JSON
    String result = "[\n" +
            "  \"{\\\"zone\\\":\\\"Tunis\\\",\\\"description\\\":\\\"le DAB fonctionne correctement.\\\",\\\"id_DAB\\\":22,\\\"etat\\\":\\\"En marche\\\",\\\"Libelle_DAB\\\":\\\"\\\"}\",\n" +
            "  \"{\\\"zone\\\":\\\"Tunis\\\",\\\"description\\\":\\\"le DAB fonctionne correctement.\\\",\\\"id_DAB\\\":26,\\\"etat\\\":\\\"En marche\\\",\\\"Libelle_DAB\\\":\\\"ooo\\\"}\",\n" +
            "  \"{\\\"zone\\\":\\\"Tunis\\\",\\\"description\\\":\\\"le DAB fonctionne correctement.\\\",\\\"id_DAB\\\":27,\\\"etat\\\":\\\"En marche\\\",\\\"Libelle_DAB\\\":\\\"\\\"}\",\n" +
            "  \"{\\\"zone\\\":\\\"Tunis\\\",\\\"description\\\":\\\"le DAB fonctionne correctement.\\\",\\\"id_DAB\\\":6660,\\\"etat\\\":\\\"En marche\\\",\\\"Libelle_DAB\\\":\\\"yyy\\\"}\"\n" +
            "]";

    String response = result.toString();

    try {
        JSONArray jArray = new JSONArray(response);

        for (int i = 0; i < jArray.length(); i++) {
            String string = jArray.getString(i);
            Log.i("try jaaray ob ", string.toString());

            JSONObject json_data = new JSONObject(string);
            int id = json_data.getInt("id_DAB");
            String libdab = json_data.getString("Libelle_DAB");
            String etat = json_data.getString("etat");
            String des= json_data.getString("description");
            String zone= json_data.getString("zone");

            Log.d("Success", "id: " + id + "\nlibdab: " + libdab + "\netat: " + etat +
            "\ndes: " + des + "\nzone: " + zone);
        }
    } catch (JSONException e) {
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
        e.printStackTrace();
    }
}
I/try jaaray ob: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":22,"etat":"En marche","Libelle_DAB":""}
D/Success: id: 22
           libdab: 
           etat: En marche
           des: le DAB fonctionne correctement.
           zone: Tunis

I/try jaaray ob: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":26,"etat":"En marche","Libelle_DAB":"ooo"}
D/Success: id: 26
           libdab: ooo
           etat: En marche    
           des: le DAB fonctionne correctement.       
           zone: Tunis

I/try jaaray ob: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":27,"etat":"En marche","Libelle_DAB":""}
D/Success: id: 27
           libdab: 
           etat: En marche
           des: le DAB fonctionne correctement.
           zone: Tunis

I/try jaaray ob: {"zone":"Tunis","description":"le DAB fonctionne correctement.","id_DAB":6660,"etat":"En marche","Libelle_DAB":"yyy"}
D/Success: id: 6660
           libdab: yyy
           etat: En marche
           des: le DAB fonctionne correctement.
           zone: Tunis