douchongbang6011 2018-01-20 08:52
浏览 115
已采纳

使用ion-koush在JsonArray.toString()上的Android null对象引用

im working on a website and android app. im using the koush/ion library im now struggling with this problem wherein the json is null, i have confirmed this with

System.out.print(result);

i am currently testing this code out to learn how jsonobject and jsonarray works in android. i am a beginner in json. im also trying to insert the jsonarray into my table view

this is the android activity

public class TestActivity extends AppCompatActivity {

TableLayout usertabletest;
static JSONArray arr = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    usertabletest = (TableLayout) findViewById(R.id.usertabletest);

    View tablehead = LayoutInflater.from(this).inflate(R.layout.testlol, null, false);
    TextView idhead = (TextView) tablehead.findViewById(R.id.idtest);
    TextView useridhead = (TextView) tablehead.findViewById(R.id.useridtest);
    TextView passwordhead = (TextView) tablehead.findViewById(R.id.passwordtest);
    TextView rolehead = (TextView) tablehead.findViewById(R.id.roletest);

    idhead.setText("id");
    useridhead.setText("userid");
    passwordhead.setText("password");
    rolehead.setText("role");

    usertabletest.addView(tablehead);

    Ion.with(this)
            .load("http://267.site11.com/doubletime-app/sql.php")
            .asJsonArray()
            .setCallback(new FutureCallback<JsonArray>() {
                @Override
                public void onCompleted(Exception e, JsonArray result) {
                    try {
                        JSONArray arr = new JSONObject(result.toString()).getJSONArray("posts");
                        // get the 'posts' section from the JSON string
                        for (int i = 0; i < arr.length(); i++) {
                            JSONObject post = arr.getJSONObject(i).getJSONObject("post");
                            String id = post.getString("id");
                            String userid = post.getString("userid");
                            String password = post.getString("password");
                            String role = post.getString("role");

                            buildtable(id, userid, password, role);
                        }
                    } catch (JSONException E) {
                        E.printStackTrace();
                    }
                }
            });
}

and this is my sql.php

<?php
    include 'JSON.php';
    $query = "SELECT * FROM user_accounts";
    encodequery($query);
?>

this is JSON.php

<?php
function encodequery($query) {
$dbhost = "localhost";
$dbuser = "id1341573_267admin";
$db = "id1341573_rotc";
$dbpass = "iloveyou267";

$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s
". $conn -> error);

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$posts = array();

if(mysqli_fetch_array($result)) {
  while($post = mysqli_fetch_assoc($result)) {
     $posts[] = array('post'=>$post);
     }
   }
encodearray($posts);
}

function encodearray($posts) {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
}

?>
  • 写回答

1条回答 默认 最新

  • duanmeng3126 2018-01-20 09:02
    关注

    Try this

    The difference between [ and { – (Square brackets and Curly brackets)

    If your JSON node starts with [, then we should use getJSONArray() method. Same as if the node starts with {, then we should use getJSONObject() method.

    Ion.with(context)
                .load(url)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        // TODO
                       try 
                       {
                          JSONArray arr  = result.getJSONArray("posts");
    
                          for (int i = 0; i < arr.length(); i++)
                          {
                             JSONObject post = arr.getJSONObject(i).getJSONObject("post");
                             String id = post.getString("id");
                             String userid = post.getString("userid");
                             String password = post.getString("password");
                             String role = post.getString("role");
                             String syncsts = post.getString("syncsts");
    
                             Log.i("ID "," "+id);
                             Log.i("User id "," "+userid);
                             Log.i("Password "," "+password);
                             Log.i("Role "," "+role);
                             Log.i("Syncsts "," "+syncsts);
    
                             buildtable(id, userid, password, role);
                         }
                    } catch (JSONException E) {
                        E.printStackTrace();
                    }
                    }
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示