2

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

JSON 输出很干净,应该可以工作!我的活动

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

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

任何帮助深表感谢 !谢谢

编辑:我的其他活动有效:

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 中,这没有任何问题,加载需要一些时间,但它可以工作。

4

2 回答 2

1

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

JSONfunctions.getJSONfromURL(params[0])

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

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

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

于 2013-03-12T11:51:07.750 回答
0

我让它工作了!这是一个简单而愚蠢的错误。我连接的服务器不允许 POST 所以在适配器中我将 Httppost 更改为 Httpget

于 2013-03-12T13:27:29.353 回答