I can successfully posting user data into the server, but I'm struggling in getting response(user registered) from the server. Please look into my code and tell me, how to get Json Response(user registered). I have tried with the Asynctask with both boolean and String type to get the Response. But in this I kept that String null. Please tell me what to do for getting the Json Response. Thanks in advance!!!
public class SignupActivity extends AppCompatActivity {
EditText firstName, lastName, emailTxt, passwordTxt;
TextView moveToLoginTxt;
Button signUpBtn;
public static String strFirstname;
public static String strLastName;
public static String strEmail;
public static String strPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Toolbar mToolbar = (Toolbar) findViewById(R.id.signuptoolbar);
mToolbar.setTitle("SignUp");
firstName = (EditText) findViewById(R.id.first_name_edittext);
lastName = (EditText) findViewById(R.id.last_name_edittext);
emailTxt = (EditText) findViewById(R.id.email_edittext);
passwordTxt = (EditText) findViewById(R.id.password_edittext);
moveToLoginTxt = (TextView) findViewById(R.id.login_textview);
signUpBtn = (Button) findViewById(R.id.btn_signup);
signUpBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strFirstname = firstName.getText().toString();
strLastName = lastName.getText().toString();
strEmail = emailTxt.getText().toString();
strPwd = passwordTxt.getText().toString();
if (TextUtils.isEmpty(strFirstname)) {
firstName.setError("Please enter username");
firstName.requestFocus();
return;
}
if (TextUtils.isEmpty(strLastName)) {
lastName.setError("Please enter your lastname");
lastName.requestFocus();
return;
}
if (TextUtils.isEmpty(strEmail)) {
emailTxt.setError("Please enter your email");
emailTxt.requestFocus();
return;
}
if (TextUtils.isEmpty(strPwd)) {
passwordTxt.setError("Please enter your password");
passwordTxt.requestFocus();
return;
}
Registruser registruser = new Registruser();
registruser.execute();
Toast.makeText(SignupActivity.this, registruser.response + "Registration Successful", Toast.LENGTH_SHORT).show();
}
});
}
public interface MyInterface {
public void myMethod(boolean result);
void onPostExecute(Boolean result);
}
public class Registruser extends AsyncTask<Void, Void, String> implements MyInterface {
private MyInterface mListener;
String data;
public String response = null;
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("my url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
public void myMethod(boolean result) {
if (result == true) {
Toast.makeText(SignupActivity.this, "Connection Succesful",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SignupActivity.this, "Connection Failed:" + result, Toast.LENGTH_LONG).show();
}
}
@Override
public void onPostExecute(Boolean result) {
if (mListener != null)
mListener.myMethod(result);
}
}
}