2016-12-03 134 views
0

我一直在收到此java.lang.String無法轉換爲JSONObject錯誤 我在jsonResponse上遇到了問題,而且我的朋友在模擬器上使用相同的代碼運行應用程序時沒有問題。Android Studio響應監聽器

public class LoginActivity extends AppCompatActivity { 

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

    final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
    final EditText etPassword = (EditText) findViewById(R.id.etPassword); 
    final Button bLogin = (Button) findViewById(R.id.bLogin); 
    final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere); 

    registerLink.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class); 
      LoginActivity.this.startActivity(registerIntent); 
     } 
    }); 

    bLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick (View v) { 
      final String username = etUsername.getText().toString(); 
      final String password = etPassword.getText().toString(); 

      Response.Listener<String> responseListener = new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 
         boolean success = jsonResponse.getBoolean("success"); 

         if (success) { 
          String name = jsonResponse.getString("name"); 
          String email = jsonResponse.getString("email"); 

          Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class); 
          intent.putExtra("name", name); 
          intent.putExtra("username", username); 
          intent.putExtra("email", email); 

          LoginActivity.this.startActivity(intent); 


         } else { 
          AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this); 
          builder.setMessage("Login Failed") 
            .setNegativeButton("Retry", null) 
            .create() 
            .show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 

        } 
       } 

      }; 
      LoginRequest loginRequest = new LoginRequest(username, password,responseListener); 
      RequestQueue queue = Volley.newRequestQueue(LoginActivity.this); 
      queue.add(loginRequest); 
     } 
    }); 
} 

}

,這裏是錯誤

W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 
W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:160) 
W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:173) 
at chengweifeng1132701116.fyp.LoginActivity$2$1.onResponse(LoginActivity.java:50) 
at chengweifeng1132701116.fyp.LoginActivity$2$1.onResponse(LoginActivity.java:46) 
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 

回答

0

嘗試登錄你server.Because得到的迴應你所得到的響應與HTML標籤
這是不是一個東西JSON響應...

+0

在我的PHP文件中存在一些問題,我修復了它..感謝您的幫助!歡呼! –