android - 如何解决这些错误 : acAppName=/system/bin/surfaceflinger and org. json.JSONObject cannot be converted to JSONArray?

标签 android runtime-error

我正在从我的移动设备上运行我的 API REST 应用程序,显然,它没有任何错误,因为它不会自动关闭或终止进程。它只是运行但没有检索到任何信息,并且在日志控制台中向我显示了两个错误。

这是我在日志控制台中看到的问题之一:acAppName=/system/bin/surfaceflinger

    06-04 16:49:21.527  17600-17600/com.example.user.project V/PhoneWindow﹕ DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{420a1478 com.example.user.project/com.example.user.project.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{4205a608 V.E..... R.....ID 0,0-0,0}
    06-04 16:49:21.527  17600-17600/com.example.user.project D/ActivityThread﹕ ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{4204b3c0 token=android.os.BinderProxy@4204aa68 {com.example.user.project/com.example.user.project.MainActivity}}
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ 0
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ appName=com.example.user.project, acAppName=/system/bin/surfaceflinger
    06-04 16:49:21.590  17600-17600/com.example.user.project E/﹕ 0
    06-04 16:49:21.592  17600-17600/com.example.user.project D/GraphicBuffer﹕ create handle(0x614af178) (w:480, h:800, f:1)
    06-04 16:49:21.593  17600-17600/com.example.user.project I/MaliEGL﹕ [Mali]window_type=1, is_framebuffer=0, errnum = 0

下面的几行我遇到了另一个问题:

06-04 17:32:24.340  23481-23503/com.example.user.project I/System.out﹕ >doSendRequest
06-04 17:32:24.341  23481-23503/com.example.user.project I/System.out﹕ <doSendRequest
06-04 17:32:24.382  23481-23503/com.example.user.project E/ServicioRest﹕ org.json.JSONException: Value {//Here the information retrieved of my JSON} of type org.json.JSONObject cannot be converted to JSONArray
06-04 17:32:24.399  23481-23481/com.example.user.project D/ListView﹕ measureHeightOfChildren adapter=com.com.example.user.project

我搜索了这两个错误,但对于第一个错误,我找不到任何对我有帮助的东西,但对于第二个错误,我找到了:org.json.JSONObject cannot be converted to JSONArray in android

我想应用 RajaReddy PolamReddy 所说的:

JSONObject object = new JSONObject(result);

   JSONArray Jarray = object.getJSONArray("contacts");

   for (int i = 0; i < Jarray.length(); i++) {
      JSONObject Jasonobject = Jarray.getJSONObject(i);

但我不知道如何将它应用到我的代码中:

 HttpClient httpClient = new DefaultHttpClient();

 HttpGet method = new HttpGet(url);

 method.setHeader("content-type", "application/json");

    try{
         HttpResponse response = httpClient.execute(method);
         String responseString = EntityUtils.toString(response.getEntity());
         JSONArray responseJSON = new JSONArray(responseString);
            for(int i=0; i<responseJSON.length(); i++){
                JSONObject object = responseJSON.getJSONObject(i);
            }
     }catch{
         Log.e("ServiceRest", ex.toString());       
     }

其中 url 是访问我的 API REST 的 GET 方法的 uri。

注意:我正在使用我的移动设备访问 uri。而不是 localhost,因为我在外部设备中,所以我使用的是我计算机的 IP 网络。我也尝试过 10.0.2.2 但它不起作用,所以请避免建议我使用 10.0.2.2 作为 IP 的答案。

注意 2:我对我的 list 有这些权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

这两个错误是否相关?我该如何解决它们?你知道为什么会出现第一个吗?

提前致谢!

编辑

我检索的 JSON 是(我简化了它,我把它检索的不同类型的数据放在这里):

int idMain = object.getInt("idMain");
String date = object.getString("date");
String name = object.getString("name");
double value = object.getDouble("value");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date datePar = sdf.parse(date);

最佳答案

问题出在选角上。您正在尝试检索一个对象并将其转换为 JsonArray,因为它是 JsonObject。 该应用程序不会崩溃,因为您用 try/catch 包围它(顺便说一句:您的代码在捕获后缺少异常)。 如果您发布您的 json 示例,我可以进一步查看并帮助您解决它。

关于android - 如何解决这些错误 : acAppName=/system/bin/surfaceflinger and org. json.JSONObject cannot be converted to JSONArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30648875/

相关文章:

android - MPAndroidChart:未调用 PieChart onValueSelected

VBA 运行时错误 '1004'

java - 从Java插入期间的Mysql日期列错误

Excel VBA运行时错误5使用超链接函数

unix - Cygwin错误消息—二进制文件不可执行

postgresql - Golang 在搜索 Postgres 时崩溃

Android SDK 不显示

android - 使用布局的选择器更改其子项的属性

android - ConstraintLayout 中的 RecyclerView 显示不正确

android - 重新创建android Activity 而不眨眼