Issue
I have this class WelcomeActivity and backgroundLogin which does a login sequence. I have my username and password editbox on the WelcomeActivity and then the login button is pressed I proceeds to the backgroungLogin to connect to the database and then compare the values of the available entries.
WelcomeActivity:
package com.a000webhostapp.cbhtermitecontrol.termitecontrol;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class WelcomeActivity extends AppCompatActivity{
Button loginButton;
EditText userText, codeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
loginButton = (Button) findViewById(R.id.loginButtonXML);
userText = (EditText) findViewById(R.id.userTextXML);
codeText = (EditText) findViewById(R.id.codeTextXML);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String username = userText.getText().toString();
String password = codeText.getText().toString();
String type = "login";
backgroundLogin backgroundLogin = new backgroundLogin(WelcomeActivity.this);
backgroundLogin.execute(type, username, password);
}
});
}
public void onLogin(View view){
String username = userText.getText().toString();
String password = codeText.getText().toString();
String type = "login";
backgroundLogin backgroundLogin = new backgroundLogin(this);
backgroundLogin.execute(type, username, password);
}
}
backgroundActivity:
package com.a000webhostapp.cbhtermitecontrol.termitecontrol;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.speech.tts.Voice;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class backgroundLogin extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
backgroundLogin (Context ctx){
this.context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "https://cbhtermitecontrol.000webhostapp.com/android/loginA2DB.php";
if(type.equals("login"))
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line;
while ((line = bufferedReader.readLine())!=null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
There are my code, however when I pressed the login button the onPre/PostExecute seem not working. Looking in the Run App Logs in Android Studio the only feed back I got after pressing the login button is this
D/libc: [NET] android_getaddrinfo_proxy get netid:0
D/libc: [NET] android_getaddrinfo_proxy-, success
Solution
Make sure to call
alertDialog.show();
In both onPreExecute and onPostExecute
Answered By - Marco
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.