2017-05-05 75 views
0

logcat的UserLoginTask的AsyncTask錯誤

05-05 16:38:06.584 7111-7334/com.example.thiago.myapplication I/System.out: Tentando logar em http://192.168.1.207/api/v2/bookdemo/_table/logins?fields=email%2Cpassword&filter=id%20%3D%201 
05-05 16:38:06.614 7111-7334/com.example.thiago.myapplication I/System.out: Connected 
05-05 16:38:06.914 7111-7334/com.example.thiago.myapplication I/System.out: After This Connection 
05-05 16:38:06.914 7111-7334/com.example.thiago.myapplication I/System.out: {"resource":[{"email":"[email protected]","password":"1234"}]} 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication I/System.out: chegou até aqui1 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err: org.json.JSONException: No value for email 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err:  at org.json.JSONObject.get(JSONObject.java:389) 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err:  at org.json.JSONObject.getString(JSONObject.java:550) 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err:  at com.example.thiago.myapplication.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:179) 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err:  at com.example.thiago.myapplication.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:98) 
05-05 16:38:06.924 7111-7111/com.example.thiago.myapplication W/System.err:  at android.os.AsyncTask.finish(AsyncTask.java:636) 

的AsyncTask

private class UserLoginTask extends AsyncTask<Void, String, String> { 

     private final String mEmail; 
     private final String mPassword; 
     //private final String mCep; 

     UserLoginTask(String email, String password) { 
      mEmail = email; 
      mPassword = password; 


     } 

     @Override 
      protected String doInBackground(Void... String) { 

      HttpURLConnection httpCon = null; 

      StringBuilder result = new StringBuilder(); 
      // Toast.makeText(LoginActivity.this,"No começo de doInbackground....",Toast.LENGTH_LONG).show(); 

       int id = 1; 

      try { 

       //URL url = new URL("https://viacep.com.br/ws/05508030/json/"); 
       String urlLogin = "http://192.168.1.207/api/v2/bookdemo/_table/logins?fields=email%2Cpassword&filter=id%20%3D%20"+id; 

       //Toast.makeText(LoginActivity.this,"Tentando logar....",Toast.LENGTH_LONG).show(); 
       System.out.println("Tentando logar em "+ urlLogin); 

       URL url = new URL(urlLogin); 
       httpCon = (HttpURLConnection) url.openConnection(); 


       httpCon.setRequestProperty("Accept", "application/json"); 
       //httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       httpCon.setRequestMethod("GET"); 
       httpCon.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"); 
       httpCon.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.fl_vmD9mHStS8o6U_WPau2Ll7QJkNbcJQaFLFwuMvGQ"); 
       httpCon.setRequestProperty("Authorization", "Basic dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3"); 
       System.out.println("Connected"); 


       InputStream in = new BufferedInputStream(httpCon.getInputStream()); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        result.append(line); 
        System.out.println("After This Connection"); 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      finally { 
       httpCon.disconnect(); 
      } 
      System.out.println(result.toString()); 

      return result.toString(); 
     } 

     @Override 
     protected void onPostExecute(final String result) { 
      /*mAuthTask = null; 
      showProgress(false); 

      if (success) { 
       finish(); 
      } else { 
       mPasswordView.setError(getString(R.string.error_incorrect_password)); 
       mPasswordView.requestFocus(); 
      }*/ 


      JSONObject json = null; 
      try { 
       json = new JSONObject(result); 
       System.out.println("chegou até aqui1"); 

       String mEmail = json.getString("email"); 
       String mPassword = json.getString("password"); 

       System.out.println("Email: "+ mEmail); 
       System.out.println("Senha: "+ mPassword); 
       System.out.println("chegou até aqui2"); 
       if (mEmail.equals(email) && mPassword.equals(password)) { 
        Intent intent = new Intent(LoginActivity.this, MainActivity2.class); 
        intent.putExtra("result", result); 
        startActivity(intent); 
       } 

       else { 
        //Toast.makeText(LoginActivity.this,"Email ou senha inválido(s)",Toast.LENGTH_LONG).show(); 
        System.out.println("Email ou senha inválido(s)"); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      // Intent intent = new Intent(LoginActivity.this, MainActivity2.class); 
      // intent.putExtra("result", result); 
      // startActivity(intent); 
     } 
    } 
} 

回答

1

你的JSON輸出看起來是這樣的。

 {"resource":[{"email":"[email protected]","password":"1234"}]} 

錯誤消息指出'org.json.JSONException: No value for email'那是因爲你想讀電子郵件直接從JSON對象值。爲了閱讀電子郵件和字符串價值,你需要閱讀資源這是一個數組,那麼遍歷數組,然後閱讀電子郵件字符串值。

JSONObject json = null; 
try { 
    json = new JSONObject(result); 
    System.out.println("chegou até aqui1"); 
    //new code reading json 
    JSONArray jArray = json.getJSONArray("resource"); 

    for(int i=0; i<jArray.length(); i++){ 
     JSONObject json_data = jArray.getJSONObject(i); 
     String mEmail = json_data.getString("email"); 
     String mPassword = json_data.getString("password"); 

     System.out.println("Email: "+ mEmail); 
     System.out.println("Senha: "+ mPassword); 

     ); 
    //end.... 

    } catch (JSONException e) { 
    e.printStackTrace(); 
}