java.lang.String类型的com.android.volley.parse错误org.json.jsonexception价值不能在Android的凌空转换为jsonArray [解决] [英] com.android.volley.parse error org.json.jsonexception value of type java.lang.string cannot be converted to jsonArray in Android Volley [SOLVED]
问题描述
当我运行这个程序,我得到这个错误。我不知道如何解决。帮我找到它请。
这是我的json_en code PHP code API。
$ I = 0;
而($行= $ result-> FETCH_ASSOC())
{
$阵列[$ i] =阵列(
news_id=> $行[news_id],
news_title=> $行[news_title],
news_abstract=> $行[news_abstract],
news_content=> $行[news_content],
news_date=> $行[news_date],
NEWS_LINK=> $行[NEWS_LINK],
news_image_link=> $行[news_image_link],
sources_name=> $行[sources_name],
CATEGORY_NAME=> $行[CATEGORY_NAME],
news_visible=> $行[news_visible]
);
$ I ++;
}
$ JSON = json_en code($数组);
和我的Android排球JsonArrayRequest的JsonArrayRequest。
JsonArrayRequest newsRequest =新JsonArrayRequest(Url.getUrlJson(),新Response.Listener< JSONArray>(){
@覆盖
公共无效onResponse(JSONArray响应){
Log.d(TAG,response.toString());
hidePDialog(); //解析JSON
的for(int i = 0; I< response.length();我++){
尝试{
JSONObject的OBJ = response.getJSONObject(I)
新闻新闻=新新闻();
如果(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(新闻);
}
赶上(JSONException E){
e.printStackTrace();
}
内容类型
< PHP
标题(内容类型:应用程序/ JSON');
请参阅从PHP脚本返回的JSON
字节顺序标记
PHP是容易返回的 BOM 的在反应,开始的时候在的.php
文件或任何其<$ C $的C>包含活动的UTF-8字符编码。
有关更多信息,请参见这个W3C答案以及部分 UTF-8 BOM在这个岗位
一位代表建议的方法prevent PHP从prepending一个BOM是<一个href=\"http://stackoverflow.com/questions/64860/best-way-to-convert-text-files-between-character-sets\">change字符从UTF-8设置你的PHP文件(S)以ASCII或ISO-8859-15。
When I run this program I get this error . I don't know how to solve . Help me finding it Please.
This is my json_encode php code 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);
and my JsonArrayRequest of Android Volley 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();
}
Content Type
<?PHP
header('Content-Type: application/json');
See Returning JSON from a PHP script.
Byte Order Marker
PHP is prone to return a BOM at the beginning of the response, when the .php
file or any of its include
s have UTF-8 character encoding.
For more information, see this W3C answer and section UTF-8 BOM in this post.
One suggested way to prevent PHP from prepending a BOM is to change the character set of your php file(s) from UTF-8 to ASCII or ISO-8859-15.
这篇关于java.lang.String类型的com.android.volley.parse错误org.json.jsonexception价值不能在Android的凌空转换为jsonArray [解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!