繁体   English   中英

来自HTTP Post Request Android的空响应

[英]Null response from 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中的细节,并且只想集中精力回复说用户存在的响应。

    <?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;
    }

它为null,因为您异步执行代码。 在HTTP请求尚未完成执行PHP脚本时,您可以干燥结果。

尝试将Toast放入AsyncTask类中的onPostExecute(String result)方法。

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Toast result. 
    }

你没有从respose获得的东西,但放入它...使用ByteArrayInputStream而不是输出流.....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM