Php JSON提要不适用于android

Php JSON提要不适用于android,php,android,json,rss,android-volley,Php,Android,Json,Rss,Android Volley,我在这里的URL上有这个提要(),在这里有这个提要() 当我在android URL中插入Feeds B时,它可以100%正常工作 但当我在android应用程序URL中插入时,它什么也得不到 这是我的密码 public class MainActivity extends Activity { private static final String TAG = MainActivity.class.getSimpleName(); private ListView listV

我在这里的URL上有这个提要(),在这里有这个提要()

当我在android URL中插入Feeds B时,它可以100%正常工作

但当我在android应用程序URL中插入时,它什么也得不到

这是我的密码

public class MainActivity extends Activity  {
    private static final String TAG = MainActivity.class.getSimpleName();
    private ListView listView;
    private FeedListAdapter listAdapter;
    private List<FeedItem> feedItems;

    //Feeds URL - Your Website URL where you uploaded the admin panel
    private String URL_FEED = "http://apps.encly.com/?feed=all_posts";
    // Session Manager Class
    SessionManagement session;
    Button btnLoadMore;

    ProgressDialog pDialog; 
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_NAME = "name";

    // Flag for current page
    int current_page = 1;


    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Getting feeds from the website into listview 
        listView = (ListView) findViewById(R.id.list);
        feedItems = new ArrayList<FeedItem>();
        listAdapter = new FeedListAdapter(this, feedItems);
        listView.setAdapter(listAdapter);


            // making fresh volley request and getting json
            JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                    URL_FEED, null, new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            VolleyLog.d(TAG, "Response: " + response.toString());
                            if (response != null) {
                                parseJsonFeed(response);
                            }
                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                        }
                    });

            // Adding request to volley request queue
            AppController.getInstance().addToRequestQueue(jsonReq);


    }

    /**
     * Parsing json reponse and passing the data to feed view list adapter
     * */
    @SuppressWarnings("deprecation")
    private void parseJsonFeed(JSONObject response) {
        try {
            JSONArray feedArray = response.getJSONArray("feed");

            for (int i = 0; i < feedArray.length(); i++) {
                JSONObject feedObj = (JSONObject) feedArray.get(i);

                FeedItem item = new FeedItem();
                item.setId(feedObj.getInt("id"));
                item.setName(feedObj.getString("fullName"));

                // Image might be null sometimes
                String image = feedObj.isNull("image") ? null : feedObj.getString("image");
                item.setImge(image);
                item.setStatus(feedObj.getString("status"));
                item.setProfilePic(feedObj.getString("profilePic"));
                item.setTimeStamp(feedObj.getString("timeStamp"));

                // url might be null sometimes
                String feedUrl = feedObj.isNull("url") ? null : feedObj
                        .getString("url");
                item.setUrl(feedUrl);

                feedItems.add(item);
            }

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

    }


    ///Menus functions
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        switch (itemId) {
        case R.id.action_logout:
            session.logoutUser();
            break;
        case R.id.myProfile:
            Intent intent2 = new Intent(this, ProfileActivity.class);
            startActivity(intent2);
            break;
        case R.id.addPhoto:
            Intent intent1 = new Intent(this, UploadPhoto.class);
            startActivity(intent1);
            break;
        }

        // TODO Auto-generated method stub
        return super.onOptionsItemSelected(item);
    }


}
我使用wordpress获取FeedA的提要,就像这里的php代码一样

     <?php
error_reporting(E_ALL);
 ini_set('display_errors', 1);
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query();
  $wp_query->query('showposts=3&post_type=usersposts'.'&paged='.$paged); 

  echo '{ "feed": [';
  $i = 1;
  while ($wp_query->have_posts()) : $wp_query->the_post(); 
  $featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   $author_id=$post->post_author;
   $ProfilePicture = get_user_meta($author_id, '_cmb_profilePic');
   $gmt_timestamp = get_the_time( 'U', $post->ID ); 
   $username = get_the_author_meta( 'user_login', $author_id );
   $firstName = get_the_author_meta( 'first_name', $author_id );
   $lastName = get_the_author_meta( 'last_name', $author_id );
  ?>

        {
            "id": <?php echo $i++; ?>,
            "name": "<?php echo $firstName." ".$lastName." - ".$username; ?>",
            "image": "<?php echo $featured_image; ?>",
            "status": "<?php echo the_title(); ?>",
            "profilePic": "<?php echo $ProfilePicture[0]; ?>",
            "timeStamp": "1403375851930",
            "url": null
        },
  <?php

  endwhile;  
  echo ']}';

  $wp_query = null; 
  $wp_query = $temp;  // Reset

  function get_avatar_url($get_avatar){
    preg_match("/src='(.*?)'/i", $get_avatar, $matches);
    return $matches[1];
  }
?>

{
“id”:,
“名称”:“,
“图像”:“,
“状态”:“状态”,
“profilePic”:“,
“时间戳”:“1403375851930”,
“url”:空
},

您的Json格式不正确。在JSON的末尾有一个额外的逗号http://apps.encly.com/?feed=all_posts
似乎不返回JSON对象。因此,您的parseJSONFeed可能无法按您希望的方式工作

您需要定义一个返回JSON对象(文件扩展名为.JSON)的URL
URL\u FEED=“yourURLhere.JSON”
是JsonObjectRequest在响应中查找JSON对象时可以接受的JSON对象。Listener()


在中,它表示JsonObjectRequest参数“listener-listener接收JSON响应”。但是当您使用
http://apps.encly.com/?feed=all_posts
,它不是JSON对象。它可能看起来像JSON对象,但不是JSON对象类型。很像Word中的
.doc
类型的文本文件看起来像
.txt
文件,但它不是一回事
.doc
中嵌入了不同的格式,您根本看不到。

如上所述,您的php生成的JSON无效。您只需要在项目之间添加
,而不需要在末尾添加。类似于以下内容的内容可以解决此问题:

echo '{ "feed": [';
$i = 1;

while ($wp_query->have_posts()) : $wp_query->the_post();
  if( $i > 1 ) 
     echo ','; 
  $featured_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   $author_id=$post->post_author;
   $ProfilePicture = get_user_meta($author_id, '_cmb_profilePic');
   $gmt_timestamp = get_the_time( 'U', $post->ID ); 
   $username = get_the_author_meta( 'user_login', $author_id );
   $firstName = get_the_author_meta( 'first_name', $author_id );
   $lastName = get_the_author_meta( 'last_name', $author_id );
  ?>

        {
            "id": <?php echo $i++; ?>,
            "name": "<?php echo $firstName." ".$lastName." - ".$username; ?>",
            "image": "<?php echo $featured_image; ?>",
            "status": "<?php echo the_title(); ?>",
            "profilePic": "<?php echo $ProfilePicture[0]; ?>",
            "timeStamp": "1403375851930",
            "url": null
        }
  <?php

endwhile;  
echo ']}';
echo'{“feed”:[;
$i=1;
而($wp_query->have_posts()):$wp_query->the_post();
如果($i>1)
回声',';
$featured_image=wp_get_attachment_url(get_post_缩略图_id($post->id));
$author\u id=$post->post\u author;
$ProfilePicture=get_user_meta($author_id,''u cmb_profilePic');
$gmt\U timestamp=获取时间('U',$post->ID);
$username=获取作者元('user\u login',$author\u id);
$firstName=获取作者元('firstName',$AUTHER\U id);
$lastName=获取作者元('lastName',$author\u id);
?>
{
“id”:,
“名称”:“,
“图像”:“,
“状态”:“状态”,
“profilePic”:“,
“时间戳”:“1403375851930”,
“url”:空
}
问题是volly库无法识别JSON数据,因为它看起来像JSON数据,但实际上不是


所以我在页面上添加了这行代码

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


现在它工作正常。

你在日志中看到了什么有用的东西吗?@hoomi用日志更新了你的JSON无效;在
“url”之后:null
你*总是添加
},
--它应该只在最后一项上,
}。@323go我修复了它,但仍然是相同的问题,我从中获得了值wordpress@YoussefAlSubaihi,如果错误,从何处获得它并不重要。我尝试复制工作提要,将其替换到另一个不工作提要,我得到相同的异常,它也不工作,您是否说您设置了
字符串URL\u提要=http://encly.com/mobile/json/feed.json“;
您仍然会得到相同的错误?我对日志
org.json.JSONException:1处的值的解释为空。
是没有值,因为它不是json对象。我将这行代码添加到页面
标题('Content-Type:application/json'))
现在它工作得很好。我从feed b复制json并将其传递到feed A,与它完全相同,但仍然不工作。您能检查一下,当您从feed b复制并粘贴内容时,是否会出现同样的异常吗?您能告诉我从这一行
VolleyLog.d(标记,“Response:+Response.toString())中看到了什么吗;
this
09-12 15:15:28.034:D/Volley(2323):[1]1.onResponse:main activity
我看不到完整的日志。你能重新粘贴它吗谢谢你,但在android端仍然存在同样的问题,没有得到任何结果:(我不认为你已经修复了它。我测试了,它仍然是损坏的JSON。我测试了,尝试在浏览器上按ctrl+f5以硬刷新或清除浏览器缓存。它仍然返回损坏的JSON。不。它仍然是无效的JSON。转到URL,复制它并将其粘贴到jsoneditoronline.org。它无效…有一个逗号不应该在那里。
header('Content-Type: application/json');