android - 无法使用 Volley 显示两个自定义 ListView

标签 android json android-layout listview android-volley

我是 android 的初学者,我正在尝试使用 Volley 创建一个自定义列表,其中包含图像列表和每个图像的文本描述。有两个 Activity 作为 ListView ,使用 volley 下载 json 数据,使用 OnItemClickListener 启动其他 Activity 。第一个 Activity 已完成运行,当我单击另一个 Activity 时,Logcat 没有显示任何内容,但第二个 Listview Activity 是空白的,只有标题... 这是 Logcat,我不知道它是否有用。

我所有的 Logcat

05-22 16:09:32.549: V/Monotype(14980): SetAppTypeFace- try to flip, app = com.wangjian.klmeet_sightseeing
05-22 16:09:32.550: V/Monotype(14980):     Typeface getFontPathFlipFont - systemFont = default
05-22 16:09:33.378: D/Volley(14980): [1] 2.onErrorResponse: MainActivity

我的代码是这样的:

第一个 Activity SightseeingActivity.java

public class SightseeingActivity extends Activity {
//Log tag
private static final String TAG = SightseeingActivity.class.getSimpleName();

//Sightseeing json url
private static final String url = "http://wangjian.site90.net/json/api_klmeet_sightseeing.json";
private ProgressDialog pDialog;
private List<Sight> sightList = new ArrayList<Sight>();
private ListView listView;
private CustomListAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sightseeing_activity);

    listView = (ListView) findViewById(R.id.sight_list);
    adapter = new CustomListAdapter(this, sightList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    //changing action bar color
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));

    //Creating volley request obj
    JsonArrayRequest sightReq = new JsonArrayRequest(url,new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response){
            Log.d(TAG, response.toString());
            pDialog.dismiss();

            for (int i = 0; i < response.length(); i++){
                try{
                    JSONObject obj = response.getJSONObject(i);
                    Sight sight = new Sight();
                    sight.setTitle(obj.getString("title"));
                    sight.setThumbnailUrl(obj.getString("image"));
                    sight.setReadmore(obj.getString("readmore"));
                    sight.setPreintroduce(obj.getString("preintroduce"));
                    sight.setTag(obj.getString("tag"));

                    sightList.add(sight);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            //notifying list adapter about data changes
            //so that it renders the list view with updated data
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error){
            VolleyLog.d(TAG, "Error: " + error.getMessage());
        }
    });
    // Adding request to request queue
            AppController.getInstance().addToRequestQueue(sightReq);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Class<? extends Activity> activityToSrart = null;
                    switch (position){
                    case 0:
                        activityToSrart = MainActivity.class;
                        break;
                    case 1:
                        activityToSrart = MerdekaSquare.class;
                        break;
                    }

                    Intent i = new Intent(getApplicationContext(), activityToSrart);
                    startActivity(i);
                }

            });
    }

 }

第二个 Activity MainActivity

public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://wangjian.site90.net/json/api_klmeet_sightseeing_face.json";

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

    listView = (ListView) findViewById(R.id.list);

    feedItems = new ArrayList<FeedItem>();

    listAdapter = new FeedListAdapter(this, feedItems);
    listView.setAdapter(listAdapter);

    // These two lines not needed,
    // just to get the look of facebook (changing background color & hiding the icon)
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
    getActionBar().setIcon(
               new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    // We first check for cached request
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Entry entry = cache.get(URL_FEED);
    if (entry != null) {
        // fetch the data from cache
        try {
            String data = new String(entry.data, "UTF-8");
            try {
                parseJsonFeed(new JSONObject(data));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    } else {
        // 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() {

                    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
 * */
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("name"));

            // 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);
        }

        // notify data changes to list adapater
        listAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

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

如果需要我会上传更多代码。 FeedListAdapter.java

@SuppressLint("InflateParams")
public class FeedListAdapter extends BaseAdapter {  
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
    this.activity = activity;
    this.feedItems = feedItems;
}

@Override
public int getCount() {
    return feedItems.size();
}

@Override
public Object getItem(int location) {
    return feedItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.feed_item, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView timestamp = (TextView) convertView
            .findViewById(R.id.timestamp);
    TextView statusMsg = (TextView) convertView
            .findViewById(R.id.txtStatusMsg);
    TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
    NetworkImageView profilePic = (NetworkImageView) convertView
            .findViewById(R.id.profilePic);
    FeedImageView feedImageView = (FeedImageView) convertView
            .findViewById(R.id.feedImage1);

    FeedItem item = feedItems.get(position);

    name.setText(item.getName());

    // Converting timestamp into x ago format
    CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
            Long.parseLong(item.getTimeStamp()),
            System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
    timestamp.setText(timeAgo);

    // Chcek for empty status message
    if (!TextUtils.isEmpty(item.getStatus())) {
        statusMsg.setText(item.getStatus());
        statusMsg.setVisibility(View.VISIBLE);
    } else {
        // status is empty, remove from view
        statusMsg.setVisibility(View.GONE);
    }

    // Checking for null feed url
    if (item.getUrl() != null) {
        url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                + item.getUrl() + "</a> "));

        // Making url clickable
        url.setMovementMethod(LinkMovementMethod.getInstance());
        url.setVisibility(View.VISIBLE);
    } else {
        // url is null, remove from the view
        url.setVisibility(View.GONE);
    }

    // user profile pic
    profilePic.setImageUrl(item.getProfilePic(), imageLoader);

    // Feed image
    if (item.getImge() != null) {
        feedImageView.setImageUrl(item.getImge(), imageLoader);
        feedImageView.setVisibility(View.VISIBLE);
        feedImageView
                .setResponseObserver(new FeedImageView.ResponseObserver() {
                    @Override
                    public void onError() {
                    }

                    @Override
                    public void onSuccess() {
                    }
                });
    } else {
        feedImageView.setVisibility(View.GONE);
    }

    return convertView;
    }

}

FeedItem

package com.wangjian.klmeet_sightseeing.model;

public class FeedItem {
private int id;
private String name, status, image, profilePic, timeStamp, url;

public FeedItem() {
}

public FeedItem(int id, String name, String image, String status,
        String profilePic, String timeStamp, String url) {
    super();
    this.id = id;
    this.name = name;
    this.image = image;
    this.status = status;
    this.profilePic = profilePic;
    this.timeStamp = timeStamp;
    this.url = url;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImge() {
    return image;
}

public void setImge(String image) {
    this.image = image;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getProfilePic() {
    return profilePic;
}

public void setProfilePic(String profilePic) {
    this.profilePic = profilePic;
}

public String getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(String timeStamp) {
    this.timeStamp = timeStamp;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
   }
}

如果需要我上传 XML 文件

feed_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/feed_bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="@dimen/feed_item_margin"
        android:layout_marginRight="@dimen/feed_item_margin"
        android:layout_marginTop="@dimen/feed_item_margin"
        android:background="@drawable/bg_parent_rounded_corner"
        android:orientation="vertical"
        android:paddingBottom="@dimen/feed_item_padding_top_bottom"
        android:paddingTop="@dimen/feed_item_padding_top_bottom" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/feed_item_padding_left_right"
            android:paddingRight="@dimen/feed_item_padding_left_right" >

            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/profilePic"
                android:layout_width="@dimen/feed_item_profile_pic"
                android:layout_height="@dimen/feed_item_profile_pic"
                android:scaleType="fitCenter" >
            </com.android.volley.toolbox.NetworkImageView>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="@dimen/feed_item_profile_info_padd" >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/feed_item_profile_name"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/timestamp"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/timestamp"
                    android:textSize="@dimen/feed_item_timestamp" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/txtStatusMsg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="@dimen/feed_item_status_pad_left_right"
            android:paddingRight="@dimen/feed_item_status_pad_left_right"
            android:paddingTop="@dimen/feed_item_status_pad_top" />

        <TextView
            android:id="@+id/txtUrl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:linksClickable="true"
            android:paddingBottom="10dp"
            android:paddingLeft="@dimen/feed_item_status_pad_left_right"
            android:paddingRight="@dimen/feed_item_status_pad_left_right"
            android:textColorLink="@color/link" />

        <info.androidhive.listviewfeed.FeedImageView
            android:id="@+id/feedImage1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:scaleType="fitXY"
            android:visibility="visible" />
    </LinearLayout>

</LinearLayout>

这是我的第一个 Activity **This is my first activity**

第二个应该是这样 **the second one should be like this**

但是 **But**

JSON

{
    "feed": [
        {
            "id": 1,
            "name": "National Geographic Channel",
            "image": "http://api.androidhive.info/feed/img/cosmos.jpg",
            "status": "\"Science is a beautiful and emotional human endeavor,\" says Brannon Braga, executive producer and director. \"And Cosmos is all about making science an experience.\"",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": null
        },
        {
            "id": 2,
            "name": "TIME",
            "image": "http://api.androidhive.info/feed/img/time_best.jpg",
            "status": "30 years of Cirque du Soleil's best photos",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg,
            "timeStamp": "1403375851930",
            "url": "http://ti.me/1qW8MLB"
        },
        {
            "id": 5,
            "name": "Abraham Lincoln",
            "image": null,
            "status": "That some achieve great success, is proof to all that others can achieve it as well",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": null
        },
        {
            "id": 3,
            "name": "Discovery",
            "image": "http://api.androidhive.info/feed/img/discovery_mos.jpg",
            "status": "A team of Austrian scientists has developed a laser system that causes fruit flies to dance.",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": "http://dsc.tv/xmMxD"
        },
        {
            "id": 4,
            "name": "Ravi Tamada",
            "image": "http://api.androidhive.info/feed/img/nav_drawer.jpg",
            "status": "Android Sliding Menu using Navigation Drawer",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": "http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/"
        },
        {
            "id": 6,
            "name": "KTM",
            "image": "http://api.androidhive.info/feed/img/ktm_1290.jpg",
            "status": "\"The Beast\" KTM 1290 Super Duke",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": ""
        },
        {
            "id": 7,
            "name": "Harley-Davidson",
            "image": "http://api.androidhive.info/feed/img/harley_bike.jpg",
            "status": "We’re assembling riders of every style, bike, and passion. If you ride with conviction, ride with us. You have 24 days to get ready for World Ride. Prepare by visiting:",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": "http://bit.ly/1wmBWaN"
        },
        {
            "id": 8,
            "name": "Rock & Girl",
            "image": "http://api.androidhive.info/feed/img/rock.jpg",
            "status": "A long time back...",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": ""
        },
        {
            "id": 8,
            "name": "Gandhi",
            "image": null,
            "status": "An eye for an eye will make the whole world blind.",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": ""
        },
        {
            "id": 9,
            "name": "LIFE",
            "image": "http://api.androidhive.info/feed/img/life_photo.jpg",
            "status": "In 1965, LIFE photographer Bill Ray spent weeks with the Hells Angels, but his amazing photos never ran in the magazine",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": "http://ti.me/1rfcQa4"
        },
        {
            "id": 10,
            "name": "Shakira",
            "image": "http://api.androidhive.info/feed/img/shakira_la_la.png",
            "status": "Download La La La (Brazil 2014) from iTunes:",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": "http://smarturl.it/FWCalbum?IQid=sh"
        },
        {
            "id": 11,
            "name": "A. R. rahman",
            "image": "http://api.androidhive.info/feed/img/ar_bw.jpg",
            "status": "",
            "profilePic": "http://wangjian.site90.net/json/klcc.jpg",
            "timeStamp": "1403375851930",
            "url": ""
        }
    ]
}

我是这方面的新手,所以任何帮助都将不胜感激。提前致谢!

最佳答案

首先, private String URL_FEED = "http://wangjian.site90.net/json/api_klmeet_sightseeing_face.json"; 在你的 MainActivity 不存在,当我在 AdvanceRestClient 中试过了,我得到了这个:

enter image description here

所以我尝试进一步查看您的 WS 的目录结构,发现没有您上面提到的任何此类 URL。

enter image description here

此外,我在尝试访问您的目录时遇到了安全问题:

enter image description here

我不确定你在认真寻找什么,但你可以尝试在 MainActivity 中使用有效的 URL,它应该可以正常工作。

安全问题让我毛骨悚然。 :(

关于android - 无法使用 Volley 显示两个自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30405345/

相关文章:

javascript - JSON 元素选择

android - 将 View 元素放在 TableLayout 的第一列

android - 从 Android 中的 JSONarray 中删除 JSONobject

java - GCM在android studio中的实现

android - 通过移动设备上的网络套接字将 Livecode native 应用程序与 Python 应用程序集成

java - 从谷歌数据流输出到谷歌云Firestore

android - 如何根据 Android 中的实际屏幕尺寸在我的 View 中放置对象?

java - Android Volley 库给出了蜂窝数据连接超时错误

android - 像文件一样读/写/dev/ttySDIO0?

php - JSON 字符串输出浮点值