2016-03-06 50 views
0

我想建立使用解析服務器上的Android手機號碼登錄由Parse.After我進入了AnyPhone,Android的指定教程我的電話號碼,該應用程序與logcat的崩潰如下。java.lang.ClassCastException:java.util.HashMap中不能轉換到org.json.JSONObject

: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 

我的LoginActivity代碼如下。

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

解析雲代碼運行意思的代碼執行正確,但我不明白爲什麼它的雲代碼crashes.The網頁版無差錯運行。

+0

callFunctionInBackground的郵編,或者這個職位可能會幫助你http://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java – IshRoid

+0

@IshRoid你意味着callFunctionInBackground的「sendCode」? –

回答

2

只需添加HashMap來new FunctionCallback<JSONObject>()

new FunctionCallback<HashMap>()和EXCUTE從那裏做新。

+1

感謝名單@savita你救了我的命 –

相關問題