我有3个文本视图,我试图用来自JSON Http响应的数据填充,但是他们当前正在使用来自响应的不正确数据填充。
由于某种原因,它没有解析字段并正确分配它们 - 它似乎只是用JSON响应中的数据开头填充每个文本视图。
{
"version": "1.0",
"encoding": "UTF-8",
"feed": {
"xmlns": "http://www.w3.org/2005/Atom",
"xmlns$openSearch": "http://a9.com/-/spec/opensearch/1.1/",
"xmlns$gd": "http://schemas.google.com/g/2005",
"xmlns$yt": "http://gdata.youtube.com/schemas/2007",
"gd$etag": "W/\"Ak8ASXk-fCp7I2A9Wh5VGEg.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comments"
},
"updated": {
"$t": "2013-12-12T14:34:08.754Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"logo": {
"$t": "http://www.gstatic.com/youtube/img/logo.png"
},
"link": [
{
"rel": "http://schemas.google.com/g/2005#feed",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?v=2"
},
{
"rel": "http://schemas.google.com/g/2005#batch",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/batch?v=2"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&max-results=50&v=2"
},
{
"rel": "service",
"type": "application/atomsvc+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=atom-service&v=2"
},
{
"rel": "next",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments?alt=json&alt=json&start-token=Gq4ECqsECDISpgQACgAAAEQAJYAEABUBAAACAJUIACgAIAABABAAABAgAAAQABAALSAAAgQQBQBAAQAgRgAQAAABAAAJBAAAAkACBAAIAACYCgAIABBAIAAACAAAQAABAEAAAAAAEAADACCAABgAggAACgIQgAAAAAEICAlRk0YAgAAAAgAASAAIEgUQAAhAgACAAIgABAIgAAAAAAAiAAAAABSMQCAgAAAAAAIAASAAgAhQggABASAAAAAiADQQiAAgSAKQGIAABAAANAAAgAGSAAgEEAhABkAAAAEhCBAAgEAoAIAIAggCgAQACCICgQAgAAAoAAAgoCQAAAAEgEEAAAgAAkEEGAECACASAAiQADAAAIKAGKAABAEAKAAAQCEBCAIAABAAAAAACGAACAIIHABACAABRAEAAQAAQgAAAwGKoABIAAAkABAEAaAJAAgIKAAACgARAARAgoJEQCBIEAABBAEAAAFEAAAyQAAAACCBgAUCAghACBIFAIAwBAAgACBFACBAgAQAFQACAEAgACBEAgBAIFgJAwBIAADECCgAQAAAUIAAgAAQAAoAIDUAgIAAABAAAAmAAgAACiACAgiBEAAAABCBAwBACQggAB4CAoAABIBAKBAACBAISBgAqAEQEIUQRAIAAADCBCAAEQCAACBAAiAAAABDCIAAAAAAQAAAAEQCBwABAAIIAAAIAgECAAAkAgQABAAQAAAAgEERCAAIAQAgAAABAAAAKOiX8qy6oLsCMLmMmejzqrsCOAFAAQ%3D%3D&max-results=50&v=2"
}
],
"author": [
{
"name": {
"$t": "YouTube"
},
"uri": {
"$t": "http://www.youtube.com/"
}
}
],
"generator": {
"$t": "YouTube data API",
"version": "2.1",
"uri": "http://gdata.youtube.com"
},
"openSearch$totalResults": {
"$t": 75
},
"openSearch$itemsPerPage": {
"$t": 50
},
"entry": [
{
"gd$etag": "W/\"A08GR347eCp7I2A9Wh5VFEQ.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs"
},
"published": {
"$t": "2013-12-08T10:50:26.000Z"
},
"updated": {
"$t": "2013-12-08T10:50:26.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "SUPER [MARIO] CHAO ..."
},
"content": {
"$t": "SUPER [MARIO] CHAO !!!!!!!!!!!!!!!!!!\ufeff"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/z133w5xy2kzrv1xjk04cfvpimyzczvw4hjs?v=2"
}
],
"author": [
{
"name": {
"$t": "Max Elymanov"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/answer37"
},
"yt$userId": {
"$t": "J4HQsbLcybybImJBXr4hmA"
}
}
],
"yt$channelId": {
"$t": "UCJ4HQsbLcybybImJBXr4hmA"
},
"yt$googlePlusUserId": {
"$t": "104133629856589391856"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"C04MRH47eCp7I2A9Wh5SFEk.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-Tb-aMCsRM"
},
"published": {
"$t": "2013-10-10T21:53:05.000Z"
},
"updated": {
"$t": "2013-10-10T21:53:05.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "oooooo the best ..."
},
"content": {
"$t": "oooooo the best saludoss manu chaoo desde ubrique ( cadiz )"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNXWhtdUX34uhfdzQqy534u2-Tb-aMCsRM?v=2"
}
],
"author": [
{
"name": {
"$t": "Juan Antonio Gago"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/u22Kbw-Z9GDEYLcuHBMX_w"
},
"yt$userId": {
"$t": "u22Kbw-Z9GDEYLcuHBMX_w"
}
}
],
"yt$channelId": {
"$t": "UCu22Kbw-Z9GDEYLcuHBMX_w"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CUEMRX47eCp7I2A9WhFaEEQ.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8"
},
"published": {
"$t": "2013-09-13T17:34:44.000Z"
},
"updated": {
"$t": "2013-09-13T17:34:44.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "peligrosamente ..."
},
"content": {
"$t": "peligrosamente excelente!!! no tengo palabras para definir lo bueno q \nsiento q este concierto es... genial!"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNls5OqMHC6zITllTb7jijbNb2XxEyaum8?v=2"
}
],
"author": [
{
"name": {
"$t": "PALOMA ROSAS"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/3myOrO39fEOgdJt9UAoP3g"
},
"yt$userId": {
"$t": "3myOrO39fEOgdJt9UAoP3g"
}
}
],
"yt$channelId": {
"$t": "UC3myOrO39fEOgdJt9UAoP3g"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CE8EQX47eCp7I2A9WhFbEEo.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw"
},
"published": {
"$t": "2013-09-01T22:00:00.000Z"
},
"updated": {
"$t": "2013-09-01T22:00:00.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "coincido 2000%"
},
"content": {
"$t": "coincido 2000%"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuMLAEj5sQvw2XuQ9mtVRvhpvmq5LsUXGJw?v=2"
}
],
"author": [
{
"name": {
"$t": "juan agus"
},
"uri": {
"$t": "http://gdata.youtube.com/feeds/api/users/0Fm76n0qFloO9YGwf9sYTg"
},
"yt$userId": {
"$t": "0Fm76n0qFloO9YGwf9sYTg"
}
}
],
"yt$channelId": {
"$t": "UC0Fm76n0qFloO9YGwf9sYTg"
},
"yt$replyCount": {
"$t": 0
},
"yt$videoid": {
"$t": "PYvyGZwHlKg"
}
},
{
"gd$etag": "W/\"CkcHSH47eCp7I2A9WhVXFEg.\"",
"id": {
"$t": "tag:youtube.com,2008:video:PYvyGZwHlKg:comment:U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ-INlE"
},
"published": {
"$t": "2012-04-15T00:33:59.000Z"
},
"updated": {
"$t": "2012-04-15T00:33:59.000Z"
},
"category": [
{
"scheme": "http://schemas.google.com/g/2005#kind",
"term": "http://gdata.youtube.com/schemas/2007#comment"
}
],
"title": {
"$t": "manu chao c'est ..."
},
"content": {
"$t": "manu chao c'est trop bon c'est du bonheur en vidéo concert yeaaaahh !!!!"
},
"link": [
{
"rel": "related",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg?v=2"
},
{
"rel": "alternate",
"type": "text/html",
"href": "http://www.youtube.com/watch?v=PYvyGZwHlKg"
},
{
"rel": "self",
"type": "application/atom+xml",
"href": "http://gdata.youtube.com/feeds/api/videos/PYvyGZwHlKg/comments/U5wNTjpckuNbsu4nD4cjeCFQCCMd-lqal5eptQ-INlE?v=2"
...
79900
0
0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
String title = getIntent().getStringExtra("title");
String uploader = getIntent().getStringExtra("uploader");
String viewCount = getIntent().getStringExtra("viewCount");
TextView titleTv = (TextView) findViewById(R.id.titleTv);
TextView uploaderTv = (TextView) findViewById(R.id.uploaderTv);
TextView viewCountTv = (TextView) findViewById(R.id.viewCountTv);
titleTv.setText(title);
uploaderTv.setText("by" + uploader + " |");
viewCountTv.setText(viewCount + " views");
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(handler , viewCount);
task.execute();
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(), "onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
String video_id = getIntent().getStringExtra("id");
player.loadVideo(video_id);
}
}
public final class GetYouTubeUserCommentsTask extends
AsyncTask<Void, Void, Void> {
public static final String LIBRARY = "CommentsLibrary";
private final Handler replyTo;
private final String username;
String video_id = getIntent().getStringExtra("id");
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(
"http://gdata.youtube.com/feeds/api/videos/"
+ video_id
+ "/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true");
HttpResponse response = client.execute(request);
String jsonString = StreamUtils.convertToString(response
.getEntity().getContent());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("feed").getJSONArray(
"entry");
List<Comments> comments = new ArrayList<Comments>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name","defaultValue");
String content = jsonObject.optString("content","defaultValue");
String published = jsonObject.optString("published","defaultValue");
comments.add(new Comments(name, content, published));
}
CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);
} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView nameTv = (TextView) findViewById(R.id.name);
nameTv.setText(com.idg.omv.domain.CommentsLibrary.getName());
TextView contentTv = (TextView) findViewById(R.id.content);
contentTv.setText(com.idg.omv.domain.CommentsLibrary.getContent());
TextView publishedTv = (TextView) findViewById(R.id.published);
publishedTv.setText(com.idg.omv.domain.CommentsLibrary.getPublished());
}
}
}
public class CommentsLibrary implements Serializable{
// The username of the owner of the comment
private static String name;
// The comment
private static String content;
// The date the comment was published
private static String published;
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
/**
* @return the user name
*/
public static String getName() {
return name;
}
/**
* @return the videos
*/
public static String getContent() {
return content;
}
/**
* @return the videos
*/
public static String getPublished() {
return published;
}
}
答案 0 :(得分:0)
你有
CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
传递给CommentsLibrary的构造函数的所有参数都是jsonString
。这三个都有相同的jsonstring。
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
因此,获取相应字段的方法时显示的内容相同
编辑:
将列表声明为实例变量
ArrayList<CommentsLibrary> list = new ArrayList<CommentsLibrary>();
然后
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name","defaultValue");
String content = jsonObject.optString("content","defaultValue");
String published = jsonObject.optString("published","defaultValue");
list.add(new CommentsLibrary(name, content, published));
}
然后在onPostExecute
使用带有自定义适配器的ListView,并使用ArrayList list
填充列表视图。
使用文本视图,您只能看到一组值。