2016-10-07 48 views
0

在這裏,我正在登錄頁面。當我寫入用戶名和移動到密碼edittext時,它應該檢查用戶名在json api中,無論是第一次登錄還是重複登錄....重複登錄時setvisible textview忘記密碼是setVisibView.visible,如果它是第一次將顯示getpassword textview。 這裏是我的代碼:使用okhttp json登錄

try { 
           JSONObject jsonObject = new JSONObject(res); 
           JSONArray result = jsonObject.getJSONArray(ConstantsValues.result); 
           JSONArray result1 = jsonObject.getJSONArray(ConstantsValues.result1); 

           for (int i = 0; i < result.length(); i++) { 
            JSONObject obj_result = result.getJSONObject(i); 
            String Success = obj_result.getString("SUCCESS"); 
            if (Success.equals("1")) { 
             for (int j = 0; j < result1.length(); j++) { 
              JSONObject obj_result1 = result1.getJSONObject(j); 
              String Message = obj_result1.getString("message"); 
              if (Message.equals("Forget Password.")) { 
               forgetpass.setVisibility(View.VISIBLE); 
              } else { 
               getpass.setVisibility(View.VISIBLE); 
              } 
             } 

            } else { 
             showAlertDialog("Invalid", "Invalid password or Email"); 
            } 


           } 
           runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             // you can access all the UI componenet 

            } 
           }); 

這裏的錯誤是:

E/JSONDemo: onResponse 
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7266) 
    at android.view.ViewRootImpl.recomputeViewAttributes(ViewRootImpl.java:3490) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.ViewGroup.recomputeViewAttributes(ViewGroup.java:1357) 
    at android.view.View.needGlobalAttributesUpdate(View.java:9904) 
    at android.view.View.setFlags(View.java:10749) 
    at android.view.View.setVisibility(View.java:7511) 
    at com.example.abhishek.focus_business_solution.SignIn$3$1.onResponse(SignIn.java:248) 
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) 
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

回答

1

假設所有代碼都在後臺線程裏面,你看到的知名度代碼應該是內部runOnUiThread():

例如:

 if (Message.equals("Forget Password.")) { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         forgetpass.setVisibility(View.VISIBLE); 
        } 
       }); 
     } 
+0

同樣使用該方法訪問任何和所有UI元素 – Kushan

+0

謝謝兄弟,我只是在搜索這隻.......我沒有得到什麼做得很容易,謝謝你.. – Abhi