Нулевой ответ от HTTP Post Request Android
Я пытаюсь сделать запрос к серверу на скрипт php, который проверит, существует ли пользователь в базе данных. В настоящее время я просто хочу убедиться, что получаю какой-то ответ. Я пытаюсь вывести значениеresponseString
когда пользователь нажимает кнопку входа в систему, но каждый раз, когда он возвращается какnull
, Кто-нибудь знает почему ??
Это моеMainActivity
public class MainActivity extends Activity {
EditText username;
EditText password;
Button loginBtn;
LinearLayout loginform;
String passwordDetail;
String usernameDetail;
String url = "http://www.mysite.com/example/checklogin.php";
String responseString = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hide the Action Bar
ActionBar ab;
ab = this.getActionBar();
ab.hide();
//Get references to XML
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
loginBtn = (Button)findViewById(R.id.loginBtn);
loginform = (LinearLayout)findViewById(R.id.loginform);
//Animation
final AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f );
AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ) ;
fadeIn.setDuration(1200);
fadeIn.setFillAfter(true);
fadeOut.setDuration(1200);
fadeOut.setFillAfter(true);
fadeOut.setStartOffset(4200+fadeIn.getStartOffset());
//Run thread after 2 seconds to start Animation
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
public void run() {
//display login form
loginform.startAnimation(fadeIn);
loginBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//display();
Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show();
if(checkLoginDetails()){
//OPENS NEW ACTIVITY
//Close splash screen
//finish();
//start home screen
Intent intent = new Intent(v.getContext(), SectionsActivity.class);
//startActivity(intent);
//creates fade in animation between two activities
overridePendingTransition(R.anim.fade_in, R.anim.splash_fade_out);
Toast.makeText(getApplicationContext(), "Login Successful" + responseString, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Login Unsuccessful", Toast.LENGTH_SHORT).show();
}
}
});
}
}, 2000);
}
//Check the login details before proceeding.
public boolean checkLoginDetails(){
usernameDetail = username.getText().toString();
passwordDetail = password.getText().toString();
new RequestTask().execute(url, usernameDetail, passwordDetail);
return true;
}
Это PHP-скрипт, который яЯ прошу - в данный момент яЯ знаю, что в db существуют жестко закодированные детали, и я просто хочу сфокусироваться на ответе, что пользователь существует.
0)
{
echo 'user found';
return true;
}
else
{
echo 'user not found';
return false;
}
?>
Это мое .AsyncTask
class RequestTask extends AsyncTask{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
responseString = null;
try {
response = httpclient.execute(new HttpPost(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}