字符串无法强制转换为JSONObject

时间:2015-07-10 01:42:20

标签: java android json parse-platform logcat

我有一个奇怪的错误,列表和解析,我无法弄清楚。每当我尝试在解析请求中查询和find()时,我有时会收到java.lang.String cannot be cast to org.json.JSONObject错误。

当我到达我的代码的这一部分时会发生这种情况:

private List<ParseUser> users; 

......
else if (viewStatus == 2) {

                Log.d("Friends view", "getting current user");
                ParseUser current = ParseUser.getCurrentUser();

                Log.d("Friends view", "Getting friend relation of user");
                ParseRelation <ParseUser> friendRelation = current.getRelation("friends");

                Log.d("Friends view", "Getting relation query");
                ParseQuery <ParseUser> queryFriends =  friendRelation.getQuery();

                Log.d("Friends view", "Getting friend query");
                try {
                    users =  queryFriends.find();
                } catch (ParseException e) {
                    Log.e("queryUsers", "error: " + e);
                }
                Log.d("Friends view", "users = query done");

            }

最奇怪的部分是,一旦我让用户在第一次注销后重新登录,我再也没有遇到过这个问题。这与ParseUser.getCurrentUser();有关吗?

错误:

07-09 20:11:35.723  17089-17089/com.curtain.projectcurtain D/List Click﹕ Position = 3
07-09 20:11:36.543      980-980/? I/SBar.NetworkController﹕ onDataActivity: direction=3
07-09 20:11:36.553      980-980/? I/SBar.NetworkController﹕ refreshSignalCluster: mobile: mHasMobileDataFeature=true DataTypeShown=zz_moto_stat_sys_data_fully_connected_4g_lte_waves_wide Activity=zz_moto_stat_sys_data_fully_inandout_waves_animation_wide in=false out=false Accessibility="4 G L T E"
07-09 20:11:38.225  17089-17241/com.curtain.projectcurtain D/Friends view﹕ getting current user
07-09 20:11:38.226  17089-17241/com.curtain.projectcurtain D/Friends view﹕ Getting friend relation of user
07-09 20:11:38.226  17089-17241/com.curtain.projectcurtain D/Friends view﹕ Getting relation query
07-09 20:11:38.226  17089-17241/com.curtain.projectcurtain D/Friends view﹕ Getting friend query
07-09 20:11:38.229  17089-17241/com.curtain.projectcurtain W/dalvikvm﹕ threadid=14: thread exiting with uncaught exception (group=0x4170dd40)
07-09 20:11:39.273      236-509/? I/SFPerfTracer﹕ triggers: (rate: 0:46) (3805837 sw vsyncs) (0 skipped) (534:1361110 vsyncs) (536:5190383)
07-09 20:11:40.557      980-980/? I/SBar.NetworkController﹕ onDataActivity: direction=0
07-09 20:11:40.564      980-980/? I/SBar.NetworkController﹕ refreshSignalCluster: mobile: Signal=zz_moto_stat_sys_signal_5bar_2_fully_wide Roaming=(none) mSimIconId=(none) Accessibility="Phone two bars.","Not roaming.",""
07-09 20:11:40.564      980-980/? I/SBar.NetworkController﹕ refreshSignalCluster: mobile: mHasMobileDataFeature=true DataTypeShown=zz_moto_stat_sys_data_fully_connected_4g_lte_waves_wide Activity=zz_moto_stat_sys_data_fully_connected_waves_none_wide in=false out=false Accessibility="4 G L T E"
07-09 20:11:41.320  17089-17241/com.curtain.projectcurtain E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    Process: com.curtain.projectcurtain, PID: 17089
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
            at com.parse.ParseRESTQueryCommand.constructParameters(ParseRESTQueryCommand.java:84)
            at com.parse.ParseRESTQueryCommand.findCommand(ParseRESTQueryCommand.java:15)
            at com.parse.NetworkQueryController.findAsync(NetworkQueryController.java:49)
            at com.parse.NetworkQueryController.findAsync(NetworkQueryController.java:27)
            at com.parse.OfflineQueryController.findAsync(OfflineQueryController.java:26)
            at com.parse.ParseQuery.findAsync(ParseQuery.java:1184)
            at com.parse.ParseQuery$3$1.then(ParseQuery.java:1176)
            at com.parse.ParseQuery$3$1.then(ParseQuery.java:1172)
            at bolts.Task$14.run(Task.java:796)
            at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
            at bolts.Task.completeAfterTask(Task.java:787)
            at bolts.Task.continueWithTask(Task.java:599)
            at bolts.Task.continueWithTask(Task.java:610)
            at bolts.Task$12.then(Task.java:702)
            at bolts.Task$12.then(Task.java:690)
            at bolts.Task$14.run(Task.java:796)
            at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
            at bolts.Task.completeAfterTask(Task.java:787)
            at bolts.Task.continueWithTask(Task.java:599)
            at bolts.Task.continueWithTask(Task.java:574)
            at bolts.Task.onSuccessTask(Task.java:690)
            at bolts.Task.onSuccessTask(Task.java:680)
            at bolts.Task.onSuccessTask(Task.java:714)
            at com.parse.ParseQuery$3.call(ParseQuery.java:1172)
            at com.parse.ParseQuery$3.call(ParseQuery.java:1169)
            at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:1108)
            at com.parse.ParseQuery.findAsync(ParseQuery.java:1169)
            at com.parse.ParseQuery.findInBackground(ParseQuery.java:1137)
            at com.parse.ParseQuery.find(ParseQuery.java:957)
            at com.curtain.projectcurtain.AddFriends$RemoteDataTask.doInBackground(AddFriends.java:106)
            at com.curtain.projectcurtain.AddFriends$RemoteDataTask.doInBackground(AddFriends.java:55)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
07-09 20:11:41.332     855-1233/? W/ActivityManager﹕ Force finishing activity com.curtain.projectcurtain/.AddFriends

AddFriends.java:

public class AddFriends extends ListActivity {
    //Global variables
    public static final int ADD_ID = Menu.FIRST;
    public static final int REQUESTS_ID = Menu.FIRST + 1;
    public static final int FRIENDS_ID = Menu.FIRST + 2;
    public static final int DELETE_ID = Menu.FIRST + 3;

    public TextView title;
    public static int viewStatus = 0;

    private List<ParseUser> users;
    private List<ParseObject> requests;

    Button btnAdd, btnRequests, btnFriends;

    //Grabbing users and putting them in the list
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {

            if (users != null){
                users = null;
            }

            //add friends view
            if (viewStatus == 0 ) {
                ParseQuery<ParseUser> queryUsers = new ParseQuery<ParseUser>("_User");
                queryUsers.orderByDescending("createdAt");
                queryUsers.whereNotEqualTo("username", ParseUser.getCurrentUser().getUsername());

                try {
                    users = queryUsers.find();
                } catch (ParseException e) {
                    Log.e("queryUsers", "error: " + e);
                }


            }
            //show requests
            else if (viewStatus == 1) {

                //grab requests only
                ParseQuery<ParseObject> queryRequests = new ParseQuery<ParseObject>("FriendRequests");
                queryRequests.whereEqualTo("to", ParseUser.getCurrentUser());
                queryRequests.whereEqualTo("status", "waiting");

                try {
                    requests = queryRequests.find();
                } catch (ParseException e) {
                    Log.e("queryUsers", "error: " + e);
                }


            }
            //View friends
            else if (viewStatus == 2) {

                Log.d("Friends view", "getting current user");
                ParseUser current = ParseUser.getCurrentUser();

                Log.d("Friends view", "Getting friend relation of user");
                ParseRelation <ParseUser> friendRelation = current.getRelation("friends");

                Log.d("Friends view", "Getting relation query");
                ParseQuery <ParseUser> queryFriends =  friendRelation.getQuery();

                Log.d("Friends view", "Getting friend query");
                try {
                    users =  queryFriends.find();
                } catch (ParseException e) {
                    Log.e("queryUsers", "error: " + e);
                }
                Log.d("Friends view", "users = query done");

            }
            return null;
        }

        //before execution
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        //while executing
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            //puts users into list view
            ArrayAdapter<String> adapter = new ArrayAdapter<String> (AddFriends.this, R.layout.friends_row);


            if (users != null && viewStatus != 1) {
                for (ParseUser user : users){
                    adapter.add((String) user.get("username"));
                }
            }
            //because requests come in as a parseObject instead of a ParseUser
            else if (viewStatus == 1 && requests != null){
                for (ParseObject request : requests){
                    adapter.add(request.getParseUser("from").getUsername());
                }
            }
            Log.d("View", "Done, setting list adapter..");


            setListAdapter(adapter);


        }
    }

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


        title = (TextView) findViewById(R.id.friendsTitle);
        title.setText("Send a friend request to one of the following:");

        new RemoteDataTask().execute();
        registerForContextMenu(getListView());

        //Add a friend
        btnAdd = (Button) findViewById(R.id.button);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title.setText("Send a friend request to one of the following:");
                viewStatus = 0;
                new RemoteDataTask().execute();
                registerForContextMenu(getListView());
            }
        });

        //View friend requests button
        btnRequests = (Button) findViewById(R.id.button2);
        btnRequests.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title.setText("Tap to accept request, hold to deny");
                viewStatus = 1;
                new RemoteDataTask().execute();
                registerForContextMenu(getListView());
            }
        });

        //Friends button
        btnFriends = (Button) findViewById(R.id.button3);
        btnFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title.setText("Your friends. Hold to delete them");
                viewStatus = 2;
                new RemoteDataTask().execute();
                registerForContextMenu(getListView());
            }
        });


    }

.....

注意:这是开发的早期阶段。现在主要只是编程功能。想尽早弄清楚这个错误。非常感谢任何帮助/建议。

提前致谢!

0 个答案:

没有答案