gpt4 book ai didi

java - com.android.volley.parse 错误 org.json.jsonexception java.lang.string 类型的值无法在 Android Volley 中转换为 jsonArray

转载 作者:行者123 更新时间:2023-11-30 02:30:32 26 4
gpt4 key购买 nike

当我运行这个程序时,我得到了这个错误。我不知道如何解决。请帮我找到它。

这是我的 json_encode php 代码 api。

  $i=0;
while ($row = $result->fetch_assoc())
{
$array[$i]=array(
"news_id" => $row["news_id"],
"news_title" => $row["news_title"],
"news_abstract" => $row["news_abstract"],
"news_content" => $row["news_content"],
"news_date" => $row["news_date"],
"news_link" => $row["news_link"],
"news_image_link" => $row["news_image_link"],
"sources_name" => $row["sources_name"],
"category_name" => $row["category_name"],
"news_visible" => $row["news_visible"]
);
$i++;
}
$json=json_encode($array, JSON_HEX_TAG|JSON_HEX_APOS);

还有我的Android Volley JsonArrayRequest的JsonArrayRequest。

JsonArrayRequest newsRequest = new JsonArrayRequest( Url.getUrlJson(), new  Response.Listener<JSONArray>(){
@Override
public void onResponse( JSONArray response ){
Log.d( TAG, response.toString() );
hidePDialog();

// Parsing json
for( int i = 0; i < response.length(); i++ ){
try{
JSONObject obj = response.getJSONObject( i );
News news = new News();
if( "1".equals( obj.getString( "news_visible" )) ){

news.setNews_id( obj.getString( "news_id" ) );
news.setNews_title( obj.getString( "news_title" ) );
news.setNews_abstract( obj.getString( "news_abstract" ) );
news.setNews_content( obj.getString( "news_content" ) );
news.setNews_date( obj.getString( "news_date" ) );
news.setNews_link( obj.getString( "news_link" ) );
news.setNews_image_link( obj.getString( "news_image_link" ));
news.setSources_name( obj.getString( "sources_name" ) );
news.setCategory_name( obj.getString( "category_name" ) );
}
newsList.add( news );
}
catch( JSONException e ){
e.printStackTrace();
}

最佳答案

内容类型

<?PHP
header('Content-Type: application/json');

参见 Returning JSON from a PHP script .

字节顺序标记

.php 文件或其任何 include 有UTF-8 字符编码。

有关详细信息,请参阅 this W3C answer和部分 UTF-8 BOM in this post .

防止 PHP 预先添加 BOM 的一种建议方法是 change the character set of your php file(s)从 UTF-8 到 ASCII 或 ISO-8859-15。

关于java - com.android.volley.parse 错误 org.json.jsonexception java.lang.string 类型的值无法在 Android Volley 中转换为 jsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com