gpt4 book ai didi

java - 解析 JSON 数组、DOCTYPE 时出错

转载 作者:行者123 更新时间:2023-11-30 11:35:00 26 4
gpt4 key购买 nike

我有一个工作 ListView ,它从我的数据库接收 JSON。但是当我尝试使用此链接时:我得到“解析数据 org.json.JSONException 时出错:值

JSON 输出是干净的,应该可以工作!我的 Activity

package com.spxc.ssa.streaming;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.spxc.ssa.streaming.task.JsonAsync;
import com.spxc.ssa.streaming.task.JsonAsync.JsonListener;

public class ListShowsController extends SherlockListActivity implements
OnClickListener {

private ProgressDialog mDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.dblist);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Shows");

JsonAsync asyncTask = new JsonAsync();
// Using an anonymous interface to listen for objects when task
// completes.
asyncTask.setJsonListener(new JsonListener() {
@Override
public void onObjectReturn(JSONObject object) {
handleJsonObject(object);
}
});
// Show progress loader while accessing network, and start async task.
mDialog = ProgressDialog.show(this, getSupportActionBar().getTitle(),
getString(R.string.loading), true);
asyncTask.execute("");

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return false;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

private void handleJsonObject(JSONObject object) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

try {

JSONArray shows = object.getJSONArray("items");

for (int i = 0; i < shows.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = shows.getJSONObject(i);

map.put("video_id", String.valueOf(i));
map.put("video_title", "" + e.getString("video_title"));
//map.put("season", "Season: " + e.getString("season"));
map.put("video_location", "" + e.getString("video_location"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.dbitems,
new String[] { "video_title", "video_location" }, new int[] { R.id.item_title,
R.id.item_subtitle });

setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);

Intent myIntent = new Intent(ListShowsController.this,
ShowsController.class);
myIntent.putExtra("video_title", o.get("video_title"));
//myIntent.putExtra("season", o.get("season"));
myIntent.putExtra("video_location", o.get("video_location"));
startActivity(myIntent);
}
});

if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}

}

异步任务

package com.spxc.ssa.streaming.task;

import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class JsonAsync extends AsyncTask<String, Void, JSONObject> {

public interface JsonListener {
public void onObjectReturn(JSONObject object);
}

public void setJsonListener(JsonListener jsonListener) {
mListener = jsonListener;
}

private JsonListener mListener;
public static final String TAG = JsonAsync.class.getSimpleName();

@Override
protected JSONObject doInBackground(String... params) {
// The argument passed into tasks are arrays. So you can pass 0 or more
// arguments, when calling from activity, it's really up to you. They
// just have to be separated by commas. Like this :
// new JsonAsync.execute("thisUrl", "thatUrl", "anotherUrl");
// Although I am only grabbing the first item in the array, by calling
// params[0] below.

JSONObject object = null;

if (params != null && params.length > 0) {
object = JSONfunctions.getJSONfromURL(params[0]);
} else {
Log.e(TAG, "Task needs an argument to be able to retrieve data.");
}

// I return the item out of this method, because it is happening of the
// UI thread, so it can;t update items on the screen. You can work with
// a database from this method. That is actually recommended.
return object;
}

@Override
protected void onPostExecute(JSONObject result) {
// This method can touch UI components without throwing an error.
if (result != null && mListener != null) {
mListener.onObjectReturn(result);
}
super.onPostExecute(result);
}
}

我以前从未见过这个错误!问题是什么?

非常感谢任何帮助!谢谢

编辑:我的其他有效 Activity :

package com.spxc.ssa.streaming;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.spxc.ssa.streaming.task.JsonAsync;
import com.spxc.ssa.streaming.task.JsonAsync.JsonListener;

public class ListMoviesController extends SherlockListActivity implements
OnClickListener {

private ProgressDialog mDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.dblist);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Movies");

JsonAsync asyncTask = new JsonAsync();
// Using an anonymous interface to listen for objects when task
// completes.
asyncTask.setJsonListener(new JsonListener() {
@Override
public void onObjectReturn(JSONObject object) {
handleJsonObject(object);
}
});
// Show progress loader while accessing network, and start async task.
mDialog = ProgressDialog.show(this, getSupportActionBar().getTitle(),
getString(R.string.loading), true);
asyncTask.execute("http://strongpixel.com/java/movies.php");

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return false;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

private void handleJsonObject(JSONObject object) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

try {

JSONArray shows = object.getJSONArray("items");

for (int i = 0; i < shows.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = shows.getJSONObject(i);

map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("name"));
//map.put("date", "Released: " + e.getString("date"));
map.put("path", "" + e.getString("path"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.dbitems,
new String[] { "name", "path" }, new int[] { R.id.item_title,
R.id.item_subtitle });

setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);

Intent myIntent = new Intent(ListMoviesController.this,
MoviesController.class);
myIntent.putExtra("name", o.get("name"));
myIntent.putExtra("season", o.get("season"));
myIntent.putExtra("path", o.get("path"));
startActivity(myIntent);
}
});

if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}

}

日志:

03-12 08:35:18.722: E/log_tag(25208): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
03-12 09:06:52.877: V/CustomViewAbove(26397): Received ACTION_DOWN
03-12 09:06:52.920: V/CustomViewAbove(26397): onInterceptTouch moved to:(30.26232, 662.0), diff:(29.26232, 1.0), mLastMotionX:1.0
03-12 09:06:52.936: V/CustomViewAbove(26397): onInterceptTouch moved to:(73.95948, 663.08905), diff:(72.95948, 0.08905029), mLastMotionX:1.0
03-12 09:06:52.940: V/CustomViewAbove(26397): this slide allowed true dx: 72.95948
03-12 09:06:52.940: V/CustomViewAbove(26397): Starting drag! from onInterceptTouch
03-12 09:06:52.993: V/SlidingMenu(26397): changing layerType. hardware? true
03-12 09:06:53.229: V/SlidingMenu(26397): changing layerType. hardware? false
03-12 09:06:54.214: V/CustomViewAbove(26397): Received ACTION_DOWN
03-12 09:06:54.450: D/dalvikvm(26397): GC_CONCURRENT freed 73K, 7% free 4495K/4812K, paused 3ms+6ms, total 24ms
03-12 09:06:55.025: E/log_tag(26397): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

编辑:我尝试了相同的 JSON,但只是在一个 txt 文件 strongpixel.com/java/JSON.txt 中,这没有任何问题,加载需要一些时间,但它有效。

最佳答案

您无法让它工作,因为您的代码无法加载正确的 JSON 字符串。只有一个地方可以下载:

JSONfunctions.getJSONfromURL(params[0])

您会收到明显的错误:无法从 <!doctype html> 这样的字符串创建 JSON 对象.这意味着您下载的是 HTML 数据而不是 JSON。对于简单测试,您可以尝试下一个代码,您将得到相同的错误:

JSONObject object = new JSONObject("<!DOCTYPE html>");

您可能会由于多种原因得到此错误响应:如果您未指定 User-Agent header 或未执行授权,则错误请求、您的防火墙、代理甚至服务器都可能返回错误结果。我确实建议使用一些 Web 调试工具来帮助您处理此类情况并确保您的应用程序接收到有效数据。如果您使用的是 Windows,我建议您使用 Fiddler,它是免费的。以下是如何为测试设备设置 fiddler 的示例:http://www.cantoni.org/2011/06/28/debug-http-android-fiddler无论如何,还有其他操作系统的替代工具。

关于java - 解析 JSON 数组、DOCTYPE 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com