Mysql配置php for android

I try this toturial . but with real host and i changed config.inc.php to below code

<?php
   $servername = "sql311.gigfa.com";
   $username = "gigfa_20021518";
   $password = "************";
   $dbname = "gigfa_20021518_hobob2";

   try {
             $conn = new PDO("mysql:host=$servername;dbname=$dbname",       $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
 catch(PDOException $e)
      {
           die("OOPs something went wrong");
       }

     ?>

and i change Async part of activity to this :

public class MainActivity extends AppCompatActivity {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
private EditText etEmail;
private EditText etPassword;

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

    // Get Reference to variables
    etEmail = (EditText) findViewById(R.id.email);
    etPassword = (EditText) findViewById(R.id.password);

}

// Triggers when LOGIN Button clicked
public void checkLogin(View arg0) {

    // Get text from email and passord field
    final String email = etEmail.getText().toString();
    final String password = etPassword.getText().toString();

        // Initialize  AsyncLogin() class with email and password
        new AsyncLogin().execute(email,password);

}

private class AsyncLogin extends AsyncTask<String, String, String>
{
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //this method will be running on UI thread
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }
    @Override
    protected String doInBackground(String... params) {
        try {

            // Enter URL address where your php file resides
            url = new URL("http://localhost/test/login.inc.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "exception";
        }
        try {
            // Setup HttpURLConnection class to send and receive data from php and mysql
            conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");

            // setDoInput and setDoOutput method depict handling of both send and receive
            conn.setDoInput(true);
            conn.setDoOutput(true);

            // Append parameters to URL
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("username", params[0])
                    .appendQueryParameter("password", params[1]);
            String query = builder.build().getEncodedQuery();

            // Open connection for sending data
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return "exception";
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return(result.toString());

            }else{

                return("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } finally {
            conn.disconnect();
        }


    }

    @Override
    protected void onPostExecute(String result) {

        //this method will be running on UI thread

        pdLoading.dismiss();

        if(result.equalsIgnoreCase("true"))
        {
            /* Here launching another activity when login successful. If you persist login state
            use sharedPreferences of Android. and logout button to clear sharedPreferences.
             */

            Intent intent = new Intent(MainActivity.this,SuccessActivity.class);
            startActivity(intent);
            MainActivity.this.finish();

        }else if (result.equalsIgnoreCase("false")){

            // If username and password does not match display a error message
            Toast.makeText(MainActivity.this, "Invalid email or password", Toast.LENGTH_LONG).Show();

        } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {

            Toast.makeText(MainActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).Show();

        }
    }

}

}

but this activity do nothing after show "loading...." . where is the problem.

dsfdfd1211
dsfdfd1211 这只是一部分。你能帮助我吗?
3 年多之前 回复
duangou1953
duangou1953 这只是测试服务器。你的意思是什么。我没有粘贴密码
3 年多之前 回复
dongxing1412
dongxing1412 警告:编写自己的访问控制层并不容易,并且有很多机会严重错误。当Laravel这样的现代开发框架内置强大的身份验证系统时,请不要编写自己的身份验证系统。绝对不要遵循建议的安全最佳实践,也不要将密码存储为纯文本。
3 年多之前 回复
doushi1847
doushi1847 为什么.inc文件会有用?你为这些人服务是可怕的。
3 年多之前 回复
dongmei5168
dongmei5168 当我查看此链接“gigfatest.gigfa.com/test/login.inc”时,它看起来不错。
3 年多之前 回复
dpbv85276
dpbv85276 没有错误。只是加载得到隐藏,没有。我认为我的问题与我的服务器验证细节有关。我不知道设置它们。
3 年多之前 回复
dongsigan2044
dongsigan2044 您需要验证您的PHP代码是否也独立于Java代码。尝试将其作为一个大块进行调试比将其分解为更小,更容易验证的代码要困难得多。你有来自PHP的任何错误消息吗?来自Java?从什么?
3 年多之前 回复
dongyingtang3803
dongyingtang3803 我编辑我的java代码comp。
3 年多之前 回复
downloadbooks_2014
downloadbooks_2014 外部链接通常会被忽略。如果它不在问题中,则不是问题的一部分。
3 年多之前 回复
donglou1866
donglou1866 你可以在链接中看到
3 年多之前 回复
douchu2823
douchu2823 这正是异步之前
3 年多之前 回复
dongyi2993
dongyi2993 显示“正在加载”的代码甚至不在此处。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐