drby30217 2016-11-11 15:49
浏览 70
已采纳

在Android登录页面无法获取php响应

Hey guys! I'm making an android login page through php and mysql. the php and mysql are working fine, but when I send user information from app to php so as to checking them, the echo turns back as a html page. as bellow:

<html>
    <body>
        <script type="text/javascript" src="/aes.js"></script>
        <script>
            function toNumbers(d) {
                var e = [];
                d.replace(/(..)/g, function(d) {
                    e.push(parseInt(d,16))
                });
                return e
            }
            function toHex() {
                for (var d = [], d = 1 == arguments.length
                        && arguments[0].constructor == Array ? arguments[0] : arguments, e = "",
                        f = 0; f < d.length; f++) {
                    e += (16 > d[f] ? "0" : "") + d[f].toString(16);
                }
                return e.toLowerCase()
            }
            var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
                b = toNumbers("98344c2eee86c3994890592585b49f80"),
                c=toNumbers("fb8ba46111eda80cc729391c1afb0580");
            document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
            location.href = "http://myloginpage.webpi.ir/login.php?i=1";
        </script>
        <noscript>This site requires Javascript to work.</noscript>
    </body>
</html>

Here is my PHP code:

<?php
$servername = "******";
$username = "******";
$password = "******";
$dbname = "******";

// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);

$username = $_POST['username'];
$password = $_POST['password'];

$query = mysqli_query($con, "SELECT * FROM tbl_user WHERE username='$username' and password='$password'");

if (mysqli_num_rows($query) > 0) {
    echo "success";
} else {
    echo "failure";
}

mysqli_close($con);
?>

my MainActivity code: note!(I have tried with both apache.http and httpconnection and both got the same result)

public class MainActivity extends ActionBarActivity {

    private EditText editTextUserName;
    private EditText editTextPassword;

    public static final String USER_NAME = "USERNAME";

    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextUserName = (EditText) findViewById(R.id.editTextUserName);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
    }

    public void invokeLogin(View view){
        username = editTextUserName.getText().toString();
        password = editTextPassword.getText().toString();

        login(username,password);

    }

    private void login(final String username, String password) {

        class LoginAsync extends AsyncTask<String, Void, String>{

            private Dialog loadingDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
            }

            @Override
            protected String doInBackground(String... params) {
                String uname = params[0];
                String pass = params[1];

                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username", uname));
                nameValuePairs.add(new BasicNameValuePair("password", pass));
                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                //    httpClient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
                    HttpPost httpPost = new HttpPost(
                            "http://myloginpage.webpi.ir/login.php/");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "
");
                    }
                    result = sb.toString();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){

                String s = result.trim();
                editTextUserName.setText(s);
                loadingDialog.dismiss();
                if(s.equalsIgnoreCase("success")){
                    Intent intent = new Intent(MainActivity.this, UserProfile.class);
                    intent.putExtra(USER_NAME, username);
                    finish();
                    startActivity(intent);
                }else {
                    Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
                }
            }
        }

        LoginAsync la = new LoginAsync();
        la.execute(username, password);

    }
}
  • 写回答

1条回答 默认 最新

  • drgwsx8405 2016-11-12 19:17
    关注

    finally I found the reason! there isn't any problem with codes. this problem occurs in some hosts, especially in free hosts. they use a security method that creates this problem.

    If you have a purchased host, and you got this problem, just contact your host provider and ask them to disable "testcookie-nginx-module".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)