从 URL 解析 JSON(随机生成)时,文本视图上不显示任何内容;(Java,Android)



我正在编写一个安卓应用程序,使用 JSON 从数据库中获取数据。PHP 文件被写入以提供来自数据库表的随机数据。此文件正在工作。这是我的php。

<?php
define('HOST','localhost');
define('USER','*********');
define('PASS','*********');
define('DB','*********');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql="SELECT `id`, `questionSin`,`answer` FROM `tf_questions` ORDER BY RAND() LIMIT 1";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'questionSin'=>$row[1],
'answer'=>$row[2]
));
}

echo json_encode(array("result"=>$result),JSON_UNESCAPED_UNICODE);

mysqli_close($con);

?>
当我得到这个 JSON 数组时。

{"result":[{"id":"13","questionSin":"KO2</sub>වල O පරමාණුවේ ඔක්සිකරණ අංකය -2 වේ.","answer":"T"}]}

(这是随机生成的(

所以我写了下面的代码来在两个文本视图中将问题和答案显示为问题答案。 以下是我的两个.java文件

主活动.java

package com.example.android.myjson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
TextView question_view;
TextView answertf_view;
Button Btngetdata;

//URL to get JSON Array
private static String url = "https://symbolistic-gyros.********.php";
//JSON Node Names
private static final String QUESTION_SIN = "questionSin";
private static final String ANSWER = "answer";
private static final String ARRAY = "results";

JSONArray user = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btngetdata = (Button)findViewById(R.id.true_button);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});



}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
// Getting JSON Array
user = json.getJSONArray(ARRAY);
JSONObject c = user.getJSONObject(0);
// Storing  JSON item in a Variable
String questionSin = c.getString(QUESTION_SIN);
String answertf1 = c.getString(ANSWER);
question_view = (TextView)findViewById(R.id.question);
answertf_view = (TextView)findViewById(R.id.answer);

//Set JSON Data in TextView
question_view.setText(questionSin);
answertf_view.setText(answertf1);

} catch (JSONException e) {
e.printStackTrace();
}

}
}
}

JSONParser.java

package com.example.android.myjson;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* Created by Yomal Amarathunge on 8/9/2017.
*/
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}

这是我的xml文件

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

<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
/>
<TextView
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.27"
android:text="True" />

</LinearLayout>

</LinearLayout>

安卓工作室没有显示我的代码的任何错误。当我在手机中安装.apk时,应用程序会运行,但文本视图上不会显示任何内容。请帮助我。谢谢。

更改

private static final String ARRAY = "results";

private static final String ARRAY = "result";

键不匹配:实际:"结果">

您提供了错误的即"结果">

最新更新