Php 无法将java.lang.String类型的值数组转换为JSONArray

Php 无法将java.lang.String类型的值数组转换为JSONArray,php,android,Php,Android,这里的问题是来自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

这里的问题是来自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;
?>
这是我的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();
            }

        }
package com.example.sandra.drapp
导入java.util.ArrayList
导入org.json.JSONArray
导入org.json.JSONException
导入android.app.Activity
导入android.app.ProgressDialog
导入android.os.Bundle
导入android.util.Log
导入android.widget.ListView
导入android.widget.Toast
导入com.android.volley.Response
导入com.android.volley.VolleyError
导入com.android.volley.VolleyLog.d
导入com.android.volley.toolbox.JsonArrayRequest
类EventsActivity:Activity(){
//日志标签
private val TAG=EventsActivity::class.java.simpleName
//事件url
私有变量url=”http://www.khazaal.tech/DoctorMobileApp/Events.php"
私有变量pDialog:ProgressDialog?=null
private var events=ArrayList()
私有变量listView:listView?=null
私有变量适配器:CustomListAdapter?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u事件)
setTitle(“事件”)
listView=findViewById(R.id.list)作为listView
adapter=CustomListAdapter(此,事件)
listView!!.setAdapter(适配器)
pDialog=ProgressDialog(此)
//在发出http请求之前显示进度对话框
pDialog!!.setMessage(“正在加载…”)
pDialog!!.show()
//创建截击请求对象
val EventsReq=JsonArrayRequest(url,对象:Response.Listener{
覆盖有趣的onResponse(响应:JSONArray){
Log.d(标记,response.toString())
hidePDialog()
//解析json
for(在响应.length()之前0中的i)
{
尝试
{
var obj=response.getJSONObject(i)
val events=events()
events.setTitle(obj.getString(“Title”))
events.setDate(obj.getInt(“日期”))
events.setTime(obj.getInt(“Time”))
events.setLocation(obj.getString(“位置”))
events.setImage\uURL(obj.getString(“image\uURL”))
}
捕获(e:JSONException){
e、 printStackTrace()
}
}
//正在通知列表适配器有关数据更改的信息
//因此,它将使用更新的数据呈现列表视图
适配器!!.notifyDataSetChanged()
}
},对象:Response.ErrorListener{
覆盖错误响应(错误:截击错误){
Toast.makeText(getApplicationContext(),error.getLocalizedMessage(),Toast.LENGTH\u LONG.show())
d(标记“Error:,Error.getLocalizedMessage())
hidePDialog()
}
})
//将请求添加到请求队列
AppController.getInstance().addToRequestQueue(EventsReq)
}
重写onDestroy(){
super.ondestory()
hidePDialog()
}
私人娱乐节目对话(){
如果(!pDialog!!.isShowing())
pDialog!!.show()
}
private fun hidePDialog(){
if(pDialog!!.isShowing())
pDialog!!.disclose();
}
}
我使用了json_编码,但它没有使用正确的json格式。。 在我的应用程序中,获取org.json.JSONException:java.lang.String类型的值数组无法转换为JSONArray,在我的json部件浏览器中,获取json数据的第1行第1列的语法错误:json.parse:意外字符


我需要一些帮助,请让我成为乞丐

删除phpScript中的这一行

print_r($data);

您的代码中有几个错误

1.在PHP代码中删除
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;
?>


您需要更改jSon格式的输出在发布的代码中,您发送jSon和非jSon数据作为响应的一部分:
echo$jSon;打印(数据)。这将破坏任何需要JSON数据的消费者;它给了我空白页1.当我删除打印($data)时;它给了我空白的页面2。我的数据库中已经有日期和时间作为int,把它们作为字符串放在这里不是错误的吗?我不是在说你是如何把它们放在这里的。从JSON输出可以清楚地看出,
2018-09-27
不是int。因此,映射到int时会出现错误。请重新格式化日期。我说的对吗?好的。。你是对的。。我会编辑这个,但当我删除打印($data)时,它会在我的浏览器和我的应用程序上给我一个空白页“@BishoyAbed”的字符0处输入结束我复制了你编辑的代码,但它也给了我一个空白页。你知道为什么空白页吗?我在想,如果来自在线主机的php脚本限制json编码……这有意义吗?或者,当我删除print\r($data)时,这并不重要;它给了我空白页
<?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;
?>
     events.setDate(obj.getInt("Date"))
     events.setTime(obj.getInt("Time"))
    events.setDate(obj.getString("Date"))
    events.setTime(obj.getString("Time"))