서버에 사용자가 데이터베이스에 있는지 확인하는 PHP 스크립트를 요청하려고합니다. 현재 나는 어떤 종류의 응답을 받고 있는지 확인하고 싶습니다. 사용자가 로그인 버튼을 누를 때마다 으로 돌아올 때마다 responseString
의 값을 출력하려고합니다. 아무도 이유를 알고 있습니까 ?? 순간 나는 열심히이 DB에 존재하는 알 세부 사항을 코딩 한 단지에 대한 응답을 다시 얻기에 초점을 맞추고 자 -HTTP Post Request Null 응답 안드로이드
이이 내가 요구하고있어 PHP 스크립트는 내 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
mysql_connect("xxx.xxx.xxx.xxx", "username", "password") or die("Couldn't select database.");
mysql_select_db("databasename") or die("Couldn't select database.");
//$username = $_POST['username'];
//$password = $_POST['password'];
$pwdMD5 = md5(123);
$sql = "SELECT * FROM membership WHERE Username = 'user1' AND Password = '$pwdMD5' ";
$result = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result);
if($numrows > 0)
{
echo 'user found';
return true;
}
else
{
echo 'user not found';
return false;
}
?>
이것은 내 AsyncTask
입니다.
class RequestTask extends AsyncTask<String, String, String>{
@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;
}
당신이 responseString''있음을 할당 할 수 ..... 대신 출력 스트림의이 InputStream를 사용 ... respose에서 무언가를 받고 있지만, 퍼팅되지 않습니다)'?? 그 문자열이'null '인지 알려주세요. – Geros
@ system32 제안한대로했는데 여전히'null'값을 반환합니다. 응용 프로그램은 결코 부서지는 것이 아니며, 단지'responseString'의 값으로'null'을 반환했습니다. – Javacadabra
'e.printStackTrace()'를'catch' 블록에 넣고 스택 추적을 게시하십시오. 도움이 될 수도 있습니다. – Geros