问题出在这里是来自php脚本还是来自我的android代码? 这是我的phpScript:
<?php
require_once 'connect.php';
$sql="SELECT * FROM events ORDER BY Eid DESC";
$result=$conn->query($sql);
$data = array();
$json = array();
if($result->num_rows >0){
while($row = $result->fetch_assoc()){
$data[] = array(
'Title' => $row["Title"],
'Date' => $row["Date"],
'Time' => $row["Time"],
'Location' => $row["Location"],
'image_url' => $row["image_url"]);
}
}
header('Content-Type:application/json');
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
echo $json;
print_r($data);
exit;
?>
在我的浏览器中输出:
Array
(
[0] => Array
(
[Title] => Pediatrics 1st Bekaa
[Date] => 2018-01-31
[Time] => 04:30:00
[Location] => Bekaa
[image_url] => http://www.khazaal.tech/DoctorMobileApp/images/2.PNG
)
[1] => Array
(
[Title] => Can this Doctor Change your life in 10 mins or less?
[Date] => 2018-02-27
[Time] => 08:30:00
[Location] => Biel
[image_url] => http://www.khazaal.tech/DoctorMobileApp/images/3.png
)
[2] => Array
(
[Title] => Basic & Advanced Ericksonian Hypnosis � NFNLP Certified
[Date] => 2018-09-27
[Time] => 06:00:00
[Location] => Zalka - Beirut
[image_url] => http://www.khazaal.tech/DoctorMobileApp/images/1.jpg
)
[3] => Array
(
[Title] => Lebanon Medical Expo 2018
[Date] => 2018-01-31
[Time] => 10:30:00
[Location] => Beirut
[image_url] => http://www.khazaal.tech/DoctorMobileApp/images/2.PNG
)
)
这是我的EventsActivity.kt(Kotlin)调用列出所有记录
package com.example.sandra.drapp
import java.util.ArrayList
import org.json.JSONArray
import org.json.JSONException
import android.app.Activity
import android.app.ProgressDialog
import android.os.Bundle
import android.util.Log
import android.widget.ListView
import android.widget.Toast
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.VolleyLog.d
import com.android.volley.toolbox.JsonArrayRequest
class EventsActivity : Activity() {
// Log tag
private val TAG = EventsActivity::class.java.simpleName
// Events url
private var url = "http://www.khazaal.tech/DoctorMobileApp/Events.php"
private var pDialog: ProgressDialog? = null
private var events = ArrayList<Events>()
private var listView: ListView? = null
private var adapter: CustomListAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_events)
setTitle("EVENTS")
listView = findViewById(R.id.list) as ListView
adapter = CustomListAdapter(this, events)
listView!!.setAdapter(adapter)
pDialog = ProgressDialog(this)
// Showing progress dialog before making http request
pDialog!!.setMessage("Loading...")
pDialog!!.show()
// Creating volley request obj
val EventsReq = JsonArrayRequest(url, object: Response.Listener<JSONArray> {
override fun onResponse(response:JSONArray) {
Log.d(TAG, response.toString())
hidePDialog()
// Parsing json
for (i in 0 until response.length())
{
try
{
var obj = response.getJSONObject(i)
val events = Events()
events.setTitle(obj.getString("Title"))
events.setDate(obj.getInt("Date"))
events.setTime(obj.getInt("Time"))
events.setLocation(obj.getString("Location"))
events.setImage_url(obj.getString("image_url"))
}
catch (e:JSONException) {
e.printStackTrace()
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter!!.notifyDataSetChanged()
}
}, object: Response.ErrorListener {
override fun onErrorResponse(error:VolleyError) {
Toast.makeText(getApplicationContext(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show()
d(TAG,"Error: ", error.getLocalizedMessage())
hidePDialog()
}
})
// Adding request to request queue
AppController.getInstance().addToRequestQueue(EventsReq)
}
override fun onDestroy() {
super.onDestroy()
hidePDialog()
}
private fun showDialog() {
if (!pDialog!!.isShowing())
pDialog!!.show()
}
private fun hidePDialog() {
if (pDialog!!.isShowing())
pDialog!!.dismiss();
}
}
我使用了json_encode但它没有使用正确的json格式.. 在我的应用程序中获取org.json.JSONException:value类型java.lang.String的数组无法转换为JSONArray在我的json部分浏览器中获取SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符
我需要一些帮助PLZ Im a begginer
答案 0 :(得分:0)
在phpScript中删除此行
print_r($data);
答案 1 :(得分:0)
您的代码中有多处错误。
在您的PHP代码中 1。删除print_r($data);
,您只需要回显$json
。
print_r()
将打印非JSON格式的数组内容。
将其更改为
<?php
require_once 'connect.php';
$sql="SELECT * FROM events ORDER BY Eid DESC";
$result=$conn->query($sql);
$data = array();
if($result->num_rows >0){
while($row = $result->fetch_assoc()){
$data[] = array(
'Title' => $row["Title"],
'Date' => $row["Date"],
'Time' => $row["Time"],
'Location' => $row["Location"],
'image_url' => $row["image_url"]);
}
}
header('Content-Type:application/json');
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
?>
2。您希望日期和时间为String而不是int。所以改变
events.setDate(obj.getInt("Date"))
events.setTime(obj.getInt("Time"))
到
events.setDate(obj.getString("Date"))
events.setTime(obj.getString("Time"))