altin bana altin bana - 1 year ago 83
JSON Question

How to parse Videos ID Name Category from Json in Android

I am trying to parse a Json file (video.js) from a http server.
this code is working fine, I get the name and the url ,so the video is playing perfect, but the only problem is I cant get the Video ID or channel number (chid) to show just before the video name.
I also would love to know how to put it in categories like Rock, RNB, POP etc. I tried this with the below json but not working. It only work if I remove categories. sorry I am a total newbie

public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
String jsonResponse = new String(response);

try {

JSONArray jsonArray = new JSONArray(jsonResponse);
JSONObject jsonObject;
String videoTitle, videoUrl, videoNum ;

int length = jsonArray.length();
for (int i = 0; i < length; ++i) {
jsonObject = (JSONObject) jsonArray.get(i);

videoTitle = jsonObject.getString("chname");
videoUrl = jsonObject.getString("chlink");
videoNum = jsonObject.getString("chid");

VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);


My Json file

{categories: [
"chid": 1,
"chname": "Video 1",
"chlogo": "",
"chlink": "http://samplevideo1.mp4",
"chenable": "",
"chnote": ""
"chid": 2,
"chname": "Video 2",
"chlogo": "",
"chlink": "http://samplevideo2.mp4",
"chenable": "",
"chnote": ""
"chid": 3,
"chname": "RO 3",
"chlogo": "",
"chlink": "http://samplevideo3.mp4",
"chenable": "",


My adapter

public class VideoSelectionListAdapter extends BaseAdapter {

private List<VideoItem> items;
private LayoutInflater inflater;

public VideoSelectionListAdapter(Context context,List<VideoItem> items) {
//items = VideoItems.getItems();

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

public int getCount() {
return items.size();

public Object getItem(int position) {
return items.get(position);

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(R.layout.simple_text_item, null);

holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(;
} else {
holder = (ViewHolder) convertView.getTag();

return convertView;

private static class ViewHolder {
TextView text;


here is my VideoItem

public class VideoItem {
String title;
int mediaNum;
String mediaUrl;

public VideoItem(String title, String mediaUrl, int mediaNum) {
this.title = title;
this.mediaUrl = mediaUrl;
this.mediaNum = mediaNum;

public String getTitle() {
return title;
public int getMediaNum() {
return mediaNum;
public String getMediaUrl() {
return mediaUrl;

Answer Source

If you would like to display an ID as well as the title, then update this line to do that.


I might also suggest making a variable for the value of items.get(position)

VideoItem v = items.get(position);
holder.text.setText(v.getMediaNum() + " - " + v.getTitle());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download