I updated the code again and after calling the API its show the null. I don't understand it why i getting this problem in my code. I parse it properly but still i getting problem in signup and login in API. How its work to show the data in the sql database?
MainActivity.java (This is a Registration form )
public class MainActivity extends AppCompatActivity {
Button register, log_in;
EditText Username, Email, Password, Mobile ;
String Username_Holder, Email_Holder, PasswordHolder, MobileHolder;
String finalResult ;
String HttpURL = "http://codexpertise.com/codexpertise.com/apitest/signup.php";
Boolean CheckEditText ;
ProgressDialog progressDialog;
HashMap<String,String> hashMap = new HashMap<>();
HttpParse httpParse = new HttpParse();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign Id'S
Username = (EditText)findViewById(R.id.username);
Email = (EditText)findViewById(R.id.Email);
Password = (EditText)findViewById(R.id.password);
Mobile = (EditText)findViewById(R.id.mobile);
register = (Button)findViewById(R.id.Submit);
log_in = (Button)findViewById(R.id.Login);
//Adding Click Listener on button.
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking whether EditText is Empty or Not
CheckEditTextIsEmptyOrNot();
if(CheckEditText){
// If EditText is not empty and CheckEditText = True then this block will execute.
UserRegisterFunction(Username_Holder,Email_Holder, PasswordHolder, MobileHolder);
}
else {
// If EditText is empty then this block will execute .
Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();
}
}
});
log_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,UserLoginActivity.class);
startActivity(intent);
}
});
}
public void CheckEditTextIsEmptyOrNot(){
Username_Holder = Username.getText().toString();
Email_Holder = Email.getText().toString();
PasswordHolder = Password.getText().toString();
MobileHolder = Mobile.getText().toString();
if(TextUtils.isEmpty(Username_Holder) || TextUtils.isEmpty(Email_Holder) || TextUtils.isEmpty(PasswordHolder) || TextUtils.isEmpty(MobileHolder))
{
CheckEditText = false;
}
else {
CheckEditText = true ;
}
}
public void UserRegisterFunction(final String Username, final String Email, final String Password, final String Mobile){
class UserRegisterFunctionClass extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this,"Loading Data",null,true,true);
}
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
progressDialog.dismiss();
Toast.makeText(MainActivity.this,httpResponseMsg.toString(), Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
hashMap.put("username",params[0]);
hashMap.put("email",params[1]);
hashMap.put("password",params[2]);
hashMap.put("mobileno",params[3]);
finalResult = httpParse.postRequest(hashMap, HttpURL);
return finalResult;
}
}
UserRegisterFunctionClass userRegisterFunctionClass = new UserRegisterFunctionClass();
userRegisterFunctionClass.execute(Username,Email,Password,Mobile);
}
}
UserLoginActivity.java
public class UserLoginActivity extends AppCompatActivity {
EditText Email, Password;
Button LogIn ;
String PasswordHolder, EmailHolder;
String finalResult ;
String HttpURL = "http://codexpertise.com/codexpertise.com/apitest/login.php";
Boolean CheckEditText ;
ProgressDialog progressDialog;
HashMap<String,String> hashMap = new HashMap<>();
HttpParse httpParse = new HttpParse();
public static final String UserEmail = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_login);
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
LogIn = (Button)findViewById(R.id.Login);
LogIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckEditTextIsEmptyOrNot();
if(CheckEditText){
UserLoginFunction(EmailHolder, PasswordHolder);
}
else {
Toast.makeText(UserLoginActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();
}
}
});
}
public void CheckEditTextIsEmptyOrNot(){
EmailHolder = Email.getText().toString();
PasswordHolder = Password.getText().toString();
if(TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))
{
CheckEditText = false;
}
else {
CheckEditText = true ;
}
}
public void UserLoginFunction(final String email, final String password){
class UserLoginClass extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(UserLoginActivity.this,"Loading Data",null,true,true);
}
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
progressDialog.dismiss();
if(httpResponseMsg.equalsIgnoreCase("Data Matched")){
finish();
Intent intent = new Intent(UserLoginActivity.this, DashboardActivity.class);
intent.putExtra(UserEmail,email);
startActivity(intent);
}
else{
Toast.makeText(UserLoginActivity.this,httpResponseMsg,Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params) {
hashMap.put("username",params[0]);
hashMap.put("password",params[1]);
finalResult = httpParse.postRequest(hashMap, HttpURL);
return finalResult;
}
}
UserLoginClass userLoginClass = new UserLoginClass();
userLoginClass.execute(email,password);
}
}
HttpParse.java
public class HttpParse {
String FinalHttpData = "";
String Result ;
BufferedWriter bufferedWriter ;
OutputStream outputStream ;
BufferedReader bufferedReader ;
StringBuilder stringBuilder = new StringBuilder();
URL url;
public String postRequest(HashMap<String, String> Data, String HttpUrlHolder) {
try {
url = new URL(HttpUrlHolder);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(14000);
httpURLConnection.setConnectTimeout(14000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
outputStream = httpURLConnection.getOutputStream();
bufferedWriter = new BufferedWriter(
new OutputStreamWriter(outputStream, "UTF-8"));
bufferedWriter.write(FinalDataParse(Data));
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
bufferedReader = new BufferedReader(
new InputStreamReader(
httpURLConnection.getInputStream()
)
);
FinalHttpData = bufferedReader.readLine();
}
else {
FinalHttpData = "Something Went Wrong";
}
} catch (Exception e) {
e.printStackTrace();
}
return FinalHttpData;
}
public String FinalDataParse(HashMap<String,String> hashMap2) throws UnsupportedEncodingException {
for(Map.Entry<String,String> map_entry : hashMap2.entrySet()){
stringBuilder.append("&");
stringBuilder.append(URLEncoder.encode(map_entry.getKey(), "UTF-8"));
stringBuilder.append("=");
stringBuilder.append(URLEncoder.encode(map_entry.getValue(), "UTF-8"));
}
Result = stringBuilder.toString();
return Result ;
}
}