Abhi Abhi - 1 month ago 9
JSON Question

Login using okhttp json

Here I am making login page.When i had written username and when move to password edittext it should check username in json api that whether it is first time login or repeatedly login....when login repeatedly done setvisible textview forget pass is setVisibView.visible and if it is first time it will show getpassword textview.
Here is my Code:

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

}
});


here error is:

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)

Answer

Assuming all your code is inside a background thread, all you view visibility code should be inside runOnUiThread() :

eg:

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