我在这个 URL 上有这个提要(提要 A),这个提要在这里(提要 B)
当我在 android URL 中插入 Feeds B 时,它工作正常 100%
但是当我在 Android 应用程序 URL 中插入 Feeds A 时,它没有得到任何东西
这是我的代码
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);
}
}
https://i.sstatic.net/VtKhy.png
提供不显示任何内容的图像
https://i.sstatic.net/PT1Iz.png
馈送 B 图像正确显示所有项目
我的Logcat
09-12 15:21:10.730: D/Volley(2426): [1] 1.onResponse: MainActivity
09-12 15:21:10.730: W/System.err(2426): org.json.JSONException: Value at 1 is null.
09-12 15:21:10.734: W/System.err(2426): at org.json.JSONArray.get(JSONArray.java:259)
09-12 15:21:10.734: W/System.err(2426): at com.twaa9l.photosee.MainActivity.parseJsonFeed(MainActivity.java:151)
09-12 15:21:10.734: W/System.err(2426): at com.twaa9l.photosee.MainActivity.access$7(MainActivity.java:146)
09-12 15:21:10.734: W/System.err(2426): at com.twaa9l.photosee.MainActivity$1.onResponse(MainActivity.java:121)
09-12 15:21:10.734: W/System.err(2426): at com.twaa9l.photosee.MainActivity$1.onResponse(MainActivity.java:1)
09-12 15:21:10.734: W/System.err(2426): at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
09-12 15:21:10.734: W/System.err(2426): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-12 15:21:10.734: W/System.err(2426): at android.os.Handler.handleCallback(Handler.java:615)
09-12 15:21:10.734: W/System.err(2426): at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 15:21:10.734: W/System.err(2426): at android.os.Looper.loop(Looper.java:137)
09-12 15:21:10.734: W/System.err(2426): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-12 15:21:10.734: W/System.err(2426): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 15:21:10.734: W/System.err(2426): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 15:21:10.734: W/System.err(2426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-12 15:21:10.734: W/System.err(2426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-12 15:21:10.734: W/System.err(2426): at dalvik.system.NativeStart.main(Native Method)
我使用 wordpress 获取 Feed A 的 feed,如这里的 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];
}
?>
http://apps.encly.com/?feed=all_posts
似乎没有返回 JSON 对象。因此,您的 parseJSONFeed 可能不会按照您希望的方式工作。
您需要定义一个返回 JSON 对象的 URL(文件扩展名为 .JSON)
URL_FEED = "yourURLhere.json"
是 JsonObjectRequest 可接受的 JSON 对象,以便在 Response.Listener() 中查找 JSON 对象。
在文档中,它表示JsonObjectRequest参数“listener - 接收JSON响应的监听器”。但是当你使用
http://apps.encly.com/?feed=all_posts
时,它就不是 JSON 对象了。它可能看起来像 JSON 对象,但它不是 JSON 对象类型。非常类似于 .doc
类型的文本文件看起来像 Word 中的 .txt
文件,但它们不是同一回事。 .doc
中嵌入了您根本看不到的不同格式。
您的 Json 格式不正确。 JSON 末尾多了一个逗号
正如我上面所说,你的 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 ']}';
请注意,我从末尾删除了逗号,并且仅在不是第一项时将其插入到开头。
问题正如 @erad 所说的那样 volly 库无法识别 JSON 数据,因为它看起来像 JSON 数据,但其实不是
所以我将这行代码添加到页面中
header('Content-Type: application/json');
现在一切正常。