Android java.lang.ClassCastException:java.util.HashMap不能强制转换为org.json.JSONObject

Android java.lang.ClassCastException:java.util.HashMap不能强制转换为org.json.JSONObject,android,json,parse-platform,login,Android,Json,Parse Platform,Login,我正试图使用Parse在AnyPhone Android教程中指定的Parse服务器在Android上建立电话号码登录。在输入我的电话号码后,应用程序与logcat崩溃,如下所示 :56:04.376 10414-10414/com.parse.anyphone E/AndroidRuntime: FATAL EXCEPTION: main Process: c

我正试图使用Parse在AnyPhone Android教程中指定的Parse服务器在Android上建立电话号码登录。在输入我的电话号码后,应用程序与logcat崩溃,如下所示

:56:04.376 10414-10414/com.parse.anyphone E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.parse.anyphone, PID: 10414
                                                                    java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
                                                                        at com.parse.anyphone.LoginActivity$2.done(LoginActivity.java:95)
                                                                        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5300)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-06 15
package com.parse.anyphone;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FunctionCallback;
import com.parse.LogInCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseUser;

import org.json.JSONObject;

import java.util.HashMap;


public class LoginActivity extends AppCompatActivity {

    private TextView questionLabel, substituteLabel;
    private EditText textField;
    private Button sendCodeButton;

    public static String phoneNumber = null;
    private String token = null;
    private int code = 0;
    private int flag = 0; //If flag = 0 call the sendCode method, otherwise call the doLogin method.

    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        progressBar= (ProgressBar) findViewById(R.id.loadingSpinner);
        questionLabel = (TextView) findViewById(R.id.questionLabel);
        substituteLabel = (TextView) findViewById(R.id.subtitleLabel);
        textField = (EditText) findViewById(R.id.phoneNumberField);
        sendCodeButton = (Button) findViewById(R.id.sendCodeButton);

        sendCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCodeButton.setClickable(false);
                if(flag == 0)
                    sendCode();
                else
                    doLogin();
            }
        });
        phoneNumberUI();
    }

    private void phoneNumberUI() {
        flag = 0;
        questionLabel.setText("Please enter your phone number to log in:");
        substituteLabel.setText("This example is limited to 10-digit US numbers");
        textField.setHint(R.string.number_default);
        sendCodeButton.setClickable(true);

    }

    private void codeUI(){
        flag = 1;
        questionLabel.setText("Enter the 4-digit confirmation code:");
        substituteLabel.setText("It was sent in an SMS message to +1" + phoneNumber);
        textField.setText("");
        textField.setHint("1234");
        sendCodeButton.setClickable(true);
    }

    private void sendCode() {
        if(textField.getText().toString().length() != 10){
            Toast.makeText(getApplicationContext(),
                    "You must enter a 10-digit US phone number including area code.",
                    Toast.LENGTH_LONG).show();
            phoneNumberUI();
        }
        else{
            phoneNumber = String.valueOf(textField.getText());

            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("phoneNumber", phoneNumber);
            params.put("language", "en");
            progressBar.setVisibility(View.VISIBLE);
            ParseCloud.callFunctionInBackground("sendCode", params, new FunctionCallback<JSONObject>() {
                public void done(JSONObject response, ParseException e) {
                    progressBar.setVisibility(View.GONE);
                    if (e == null) {
                        Log.d("Cloud Response", "There were no exceptions! " + response.toString());
                        codeUI();
                    } else {
                        Log.d("Cloud Response", "Exception: " + response.toString() + e);
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong.  Please try again." + e,
                                Toast.LENGTH_LONG).show();
                        phoneNumberUI();
                    }
                }
            });
        }
    }

    private void doLogin() {
        if(textField.getText().toString().length() != 4) {
            Toast.makeText(getApplicationContext(),
                    "You must enter the 4 digit code texted to your phone number.",
                    Toast.LENGTH_LONG).show();
            codeUI();
        } else {
            code = Integer.parseInt(textField.getText().toString());
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("phoneNumber", phoneNumber);
            params.put("codeEntry", code);

            progressBar.setVisibility(View.VISIBLE);
            ParseCloud.callFunctionInBackground("logIn", params, new FunctionCallback<String>() {
                public void done(String response, ParseException e) {
                    progressBar.setVisibility(View.GONE);
                    if (e == null) {
                        token = response;
                        Log.d("Cloud Response", "There were no exceptions! " + response);
                        ParseUser.becomeInBackground(token, new LogInCallback() {
                            @Override
                            public void done(ParseUser parseUser, ParseException e) {
                                if (e == null){
                                    Log.d("Cloud Response", "There were no exceptions! ");
                                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(i);
                                    finish();
                                }
                                else {
                                    Log.d("Cloud Response", "Exception: " + e);
                                    Toast.makeText(getApplicationContext(),
                                            "Something went wrong.  Please try again." + e,
                                            Toast.LENGTH_LONG).show();
                                    phoneNumberUI();
                                }
                            }
                        });
                    } else {
                        phoneNumberUI();
                        Log.d("Cloud Response", "Exception: " + response + e);
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong.  Please try again.",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
我的LoginActivity代码如下

:56:04.376 10414-10414/com.parse.anyphone E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.parse.anyphone, PID: 10414
                                                                    java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
                                                                        at com.parse.anyphone.LoginActivity$2.done(LoginActivity.java:95)
                                                                        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5300)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-06 15
package com.parse.anyphone;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FunctionCallback;
import com.parse.LogInCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseUser;

import org.json.JSONObject;

import java.util.HashMap;


public class LoginActivity extends AppCompatActivity {

    private TextView questionLabel, substituteLabel;
    private EditText textField;
    private Button sendCodeButton;

    public static String phoneNumber = null;
    private String token = null;
    private int code = 0;
    private int flag = 0; //If flag = 0 call the sendCode method, otherwise call the doLogin method.

    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        progressBar= (ProgressBar) findViewById(R.id.loadingSpinner);
        questionLabel = (TextView) findViewById(R.id.questionLabel);
        substituteLabel = (TextView) findViewById(R.id.subtitleLabel);
        textField = (EditText) findViewById(R.id.phoneNumberField);
        sendCodeButton = (Button) findViewById(R.id.sendCodeButton);

        sendCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendCodeButton.setClickable(false);
                if(flag == 0)
                    sendCode();
                else
                    doLogin();
            }
        });
        phoneNumberUI();
    }

    private void phoneNumberUI() {
        flag = 0;
        questionLabel.setText("Please enter your phone number to log in:");
        substituteLabel.setText("This example is limited to 10-digit US numbers");
        textField.setHint(R.string.number_default);
        sendCodeButton.setClickable(true);

    }

    private void codeUI(){
        flag = 1;
        questionLabel.setText("Enter the 4-digit confirmation code:");
        substituteLabel.setText("It was sent in an SMS message to +1" + phoneNumber);
        textField.setText("");
        textField.setHint("1234");
        sendCodeButton.setClickable(true);
    }

    private void sendCode() {
        if(textField.getText().toString().length() != 10){
            Toast.makeText(getApplicationContext(),
                    "You must enter a 10-digit US phone number including area code.",
                    Toast.LENGTH_LONG).show();
            phoneNumberUI();
        }
        else{
            phoneNumber = String.valueOf(textField.getText());

            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("phoneNumber", phoneNumber);
            params.put("language", "en");
            progressBar.setVisibility(View.VISIBLE);
            ParseCloud.callFunctionInBackground("sendCode", params, new FunctionCallback<JSONObject>() {
                public void done(JSONObject response, ParseException e) {
                    progressBar.setVisibility(View.GONE);
                    if (e == null) {
                        Log.d("Cloud Response", "There were no exceptions! " + response.toString());
                        codeUI();
                    } else {
                        Log.d("Cloud Response", "Exception: " + response.toString() + e);
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong.  Please try again." + e,
                                Toast.LENGTH_LONG).show();
                        phoneNumberUI();
                    }
                }
            });
        }
    }

    private void doLogin() {
        if(textField.getText().toString().length() != 4) {
            Toast.makeText(getApplicationContext(),
                    "You must enter the 4 digit code texted to your phone number.",
                    Toast.LENGTH_LONG).show();
            codeUI();
        } else {
            code = Integer.parseInt(textField.getText().toString());
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("phoneNumber", phoneNumber);
            params.put("codeEntry", code);

            progressBar.setVisibility(View.VISIBLE);
            ParseCloud.callFunctionInBackground("logIn", params, new FunctionCallback<String>() {
                public void done(String response, ParseException e) {
                    progressBar.setVisibility(View.GONE);
                    if (e == null) {
                        token = response;
                        Log.d("Cloud Response", "There were no exceptions! " + response);
                        ParseUser.becomeInBackground(token, new LogInCallback() {
                            @Override
                            public void done(ParseUser parseUser, ParseException e) {
                                if (e == null){
                                    Log.d("Cloud Response", "There were no exceptions! ");
                                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(i);
                                    finish();
                                }
                                else {
                                    Log.d("Cloud Response", "Exception: " + e);
                                    Toast.makeText(getApplicationContext(),
                                            "Something went wrong.  Please try again." + e,
                                            Toast.LENGTH_LONG).show();
                                    phoneNumberUI();
                                }
                            }
                        });
                    } else {
                        phoneNumberUI();
                        Log.d("Cloud Response", "Exception: " + response + e);
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong.  Please try again.",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
package com.parse.anyphone;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ProgressBar;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.parse.FunctionCallback;
导入com.parse.LogInCallback;
导入com.parse.ParseCloud;
导入com.parse.ParseException;
导入com.parse.ParseUser;
导入org.json.JSONObject;
导入java.util.HashMap;
公共类LoginActivity扩展了AppCompatActivity{
私有文本视图问题标签,替代标签;
私有编辑文本字段;
私人按钮sendCodeButton;
公共静态字符串phoneNumber=null;
私有字符串标记=null;
私有整数码=0;
private int flag=0;//如果flag=0,则调用sendCode方法,否则调用doLogin方法。
私人ProgressBar ProgressBar;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
progressBar=(progressBar)findViewById(R.id.loadingSpinner);
questionLabel=(TextView)findViewById(R.id.questionLabel);
substituteLabel=(TextView)findViewById(R.id.subtitleLabel);
textField=(EditText)findViewById(R.id.phoneNumberField);
sendCodeButton=(按钮)findViewById(R.id.sendCodeButton);
sendCodeButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
sendCodeButton.setClickable可设置(假);
如果(标志==0)
sendCode();
其他的
多洛金();
}
});
phoneNumberUI();
}
私有void phoneNumberUI(){
flag=0;
questionLabel.setText(“请输入您要登录的电话号码:”);
substituteLabel.setText(“此示例仅限于10位美国数字”);
textField.setHint(R.string.number\u默认值);
sendCodeButton.setClickable(真);
}
私有void codeUI(){
flag=1;
questionLabel.setText(“输入4位确认码:”);
substituteLabel.setText(“它通过短信发送到+1”+电话号码);
textField.setText(“”);
textField.setHint(“1234”);
sendCodeButton.setClickable(真);
}
私有void sendCode(){
if(textField.getText().toString().length()!=10){
Toast.makeText(getApplicationContext(),
“您必须输入包含区号的10位美国电话号码。”,
Toast.LENGTH_LONG).show();
phoneNumberUI();
}
否则{
phoneNumber=String.valueOf(textField.getText());
HashMap params=新的HashMap();
参数put(“电话号码”,电话号码);
参数put(“语言”、“英语”);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground(“sendCode”,参数,新函数回调(){
public void done(JSONObject响应,parsee异常){
progressBar.setVisibility(View.GONE);
如果(e==null){
Log.d(“云响应”,“没有异常!”+Response.toString());
codeUI();
}否则{
Log.d(“云响应”,“异常:”+Response.toString()+e);
Toast.makeText(getApplicationContext(),
“出现问题,请再试一次。”+e,
Toast.LENGTH_LONG).show();
phoneNumberUI();
}
}
});
}
}
私有void doLogin(){
if(textField.getText().toString().length()!=4){
Toast.makeText(getApplicationContext(),
“您必须输入发送到您的电话号码的4位代码。”,
Toast.LENGTH_LONG).show();
codeUI();
}否则{
code=Integer.parseInt(textField.getText().toString());
HashMap params=新的HashMap();
参数put(“电话号码”,电话号码);
参数put(“代码输入”,代码);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground(“登录”,参数,新函数回调(){
public void done(字符串响应,parsee异常){
progressBar.setVisibility(View.GONE);
如果(e==null){
令牌=响应;
Log.d(“云响应”,“没有异常!”+响应);
becomeInBackground(令牌,新LogInCallback(){
@凌驾
公共无效完成(ParseUser ParseUser,parsee异常){
如果(e==null){
Log.d(“云响应”,“没有异常!”);
意图i=新意图(LoginActivity.this、MainActivity.class);
星触觉(i);
完成();
}
否则{
Log.d(“云响应”,“异常:+e”);
Toast.makeText(getApplicationCo