Android Studio Mobile Coding ERROR

JC0133

Senior member
Nov 2, 2010
201
1
76
So, I am trying to write up some code where I connect TO MS SQL SERVER, which I did get to work.



I know this worked as I was able to test what was in my database by querying it. It shows a successful connection when I query something that is there and when something isn’t is says invalid credentials.



The problem is after that the next thing I wanted to test, was setText in the editText fields right after I do a successful query, but I keep getting an error. Saying something about it" must be apart of the main thread" but the code is IN the same thread.

I even put two test cases in different spots IN the thread and it doesn’t work. I commented it out and made code bigger/bold below. Can someone please help me and tell me why I can’t set The Text in the editText field in the UI, and please give me a possible solution.




I need to edit the text fields once I do a successful query with the data in the MS SQL database/table.


public class MainActivity extends Activity
{
ConnectionClass connectionClass;
EditText edtuserid,edtpass,edBrFist,edBrFeet;
Button btnlogin;
ProgressBar pbbar;

String hold = "1";

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

connectionClass = new ConnectionClass();
edBrFist =(EditText) findViewById(R.id.brainSfistText);
edBrFeet =(EditText) findViewById(R.id.brainSfeetText);
edtuserid = (EditText) findViewById(R.id.et_username);
edtpass = (EditText) findViewById(R.id.et_password);
btnlogin = (Button) findViewById(R.id.btn_Login);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);





btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DoLogin doLogin = new DoLogin();
doLogin.execute("");


}
});

}


public class DoLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;


String userid = edtuserid.getText().toString();
String password = edtpass.getText().toString();


@Override
protected void onPreExecute() {
pbbar.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(String r) {
pbbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();

if(isSuccess) {
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
}

}

@Override
protected String doInBackground(String... params) {
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Please enter User Id and Password";
else
{
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from BraiNetDataSet where UserID='" + userid + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

if(rs.next())
{

z = "Login successfull";

// edBrFeet.setText("Test1");


isSuccess=true;
}
else
{
z = "Invalid Credentials";
isSuccess = false;
}

}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
}
}

return z;
}

}

// edBrFist.setText("Test2");
}
 

MarkLuvsCS

Senior member
Jun 13, 2004
740
0
76
You can only make changes to the UI in the main thread. Your async task is in its own thread. The onPostExecute is where you need to put the UI changes since that will run on the main thread after the async task is complete.

I believe you can use [ code ] [ /code] tags to keep some of the formatting.
 

AMDisTheBEST

Senior member
Dec 17, 2015
682
90
61
I usually just use runnable to spun off background process on another thread and then update the UI with a handler.
I never used AsyncTask before.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |