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);
}
}
}
You have to create an InputStreamReader that reads your request input. Next, create a BufferedReader that will allow to iterate through the response. Then iterate through each line of our response and append it to your StringBuilder.
@Override
protected String doInBackground(Void... params){
String result;
String inputLine;
try {
//Create a URL object holding url
URL myUrl = new URL("your url");
//Create a connection
HttpURLConnection connection =(HttpURLConnection)
myUrl.openConnection();
//Set methods and timeouts
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
//Connect to url
connection.connect()
//Create a new InputStreamReader
InputStreamReader streamReader = new
InputStreamReader(connection.getInputStream());
//Create a new buffered reader and String Builder
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
//Close InputStream and Buffered reader
reader.close();
streamReader.close();
//Set result equal to stringBuilder
result = stringBuilder.toString();
}
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.