Android JsonArray.toString() 上使用 ion-koush 的空对象引用
Android null object reference on JsonArray.toString() using ion-koush
我正在开发网站和 android 应用程序。我正在使用 koush/ion 库,我现在正在努力解决这个问题,其中 json 为空,我已经用
确认了这一点
System.out.print(result);
我目前正在测试此代码以了解 jsonobject 和 jsonarray 如何在 android 中工作。我是 json 的初学者。我还试图将 json 数组插入我的 table 视图
这是androidactivity
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();
}
}
});
}
这是我的 sql.php
<?php
include 'JSON.php';
$query = "SELECT * FROM user_accounts";
encodequery($query);
?>
这是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\n". $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));
}
?>
试试这个
[和{的区别-(方括号和大括号)
如果您的 JSON 节点以 [ 开头,那么我们应该使用 getJSONArray() 方法。同理节点以{开头,那么我们应该使用getJSONObject()方法。
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();
}
}
});
我正在开发网站和 android 应用程序。我正在使用 koush/ion 库,我现在正在努力解决这个问题,其中 json 为空,我已经用
确认了这一点System.out.print(result);
我目前正在测试此代码以了解 jsonobject 和 jsonarray 如何在 android 中工作。我是 json 的初学者。我还试图将 json 数组插入我的 table 视图
这是androidactivity
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();
}
}
});
}
这是我的 sql.php
<?php
include 'JSON.php';
$query = "SELECT * FROM user_accounts";
encodequery($query);
?>
这是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\n". $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));
}
?>
试试这个
[和{的区别-(方括号和大括号)
如果您的 JSON 节点以 [ 开头,那么我们应该使用 getJSONArray() 方法。同理节点以{开头,那么我们应该使用getJSONObject()方法。
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();
}
}
});