似乎无法将我的 android 连接到我的 php 服务器

Can't seem to connect my android to my php server

我是 android 开发的新手,我一直致力于此项目以创建登录功能。但我似乎无法从我的服务器检索信息。无法让我的代码工作。

Login.java

package com.httpkhronolog.khronolog;

import android.app.ActionBar;
import android.app.Notification;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

public class Login_Activity extends AppCompatActivity {

    public static final String SERVER_ADDRESS = "http://www.khronolog.com/mobile/";
    public static final String TAG_LOGINSTATUS = "loginStatus";
    EditText etUsername, etPassword;
    ImageButton ibLogin;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        context = this;

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        etUsername = (EditText) findViewById(R.id.etUsername);
        etPassword = (EditText) findViewById(R.id.etPassword);
        ibLogin = (ImageButton)findViewById(R.id.ibLogin);

        ibLogin.setOnClickListener(new View.OnClickListener(){
           public void onClick(View v){
               String username = etUsername.getText().toString();
               String password = etPassword.getText().toString();

               if(username.length() == 0 || password.length() == 0){
                   Toast toast = Toast.makeText(context, "Username or password not entered", Toast.LENGTH_SHORT);
                   toast.show();
                   return;
               }else
               {
                    new checkUserDetails().execute(username, password);
               }
           }
        });

    }

    public class checkUserDetails extends AsyncTask<String, Boolean, Boolean>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) {


            DataOutputStream printout;
            HttpURLConnection urlConn;
            BufferedReader reader;
            URL url;
            String json;
            JSONObject jObj;
            Boolean JSONLogin = false;

            //send POST login details to server
            try {
                url = new URL(SERVER_ADDRESS + "login.php");
                urlConn = (HttpURLConnection) url.openConnection();
                urlConn.setRequestMethod("POST");
                urlConn.setDoOutput(true);
                urlConn.setRequestProperty("Accept-Charset", "UTF-8");
                urlConn.setReadTimeout(10000);
                urlConn.setConnectTimeout(15000);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("username", params[0]);
                map.put("password", params[1]);

                StringBuilder sb = new StringBuilder();
                for(HashMap.Entry<String, String> e : map.entrySet()){
                    if(sb.length() > 0){
                        sb.append("&");
                    }
                    sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8"));
                }


                String loginDetails = sb.toString();

                printout = new DataOutputStream(urlConn.getOutputStream());
                printout.writeBytes(loginDetails);
                printout.flush();
                printout.close();

                reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                StringBuilder sb2 = new StringBuilder();
                String result = null;
                while((result=reader.readLine())!=null) {
                    sb2.append(result + '\n');
                    Log.d("Results", sb2.toString());
                };

                json = result.toString();

                jObj = new JSONObject(json);


                JSONLogin = jObj.getBoolean(TAG_LOGINSTATUS);
                Log.d("LoginStatus!!!!", JSONLogin.toString());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                Log.d("JsonConvertion", "FAILED TO CONVERT JSON");
            }


            return JSONLogin;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }




    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login_, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    */
}

login.php 在服务器上

<?php
if (isset($_POST['username'])) {
        // Define $username and $password
        $username=$_POST['username'];
        $password=$_POST['password'];
        // Establishing Connection with Server by passing server_name, user_id and password as a parameter
        $connection = mysql_connect("localhost", "SomeAdmin", "somepassword");
        // To protect MySQL injection for Security purpose
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysql_real_escape_string($username);
        $password = mysql_real_escape_string($password);
        // Selecting Database
        $db = mysql_select_db("khronolo_systemdb", $connection);
        // SQL query to fetch information of registerd users and finds user match.
        $query = mysql_query("select * from user_table where password='$password' AND email='$username'", $connection);
        $rows = mysql_num_rows($query);
        if ($rows == 1) {
            $returnValue[] = array("loginStatus"=> "true");
            echo json_encode($returnValue);
        }
        else {
            $returnValue[] = array("loginStatus"=> "false");
            echo json_encode($returnValue);
        }
        mysql_close($connection); // Closing Connection
    }
}

?>

堆栈跟踪错误

    10-18 18:12:05.310 2594-2711/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime: Process: com.httpkhronolog.khronolog, PID: 2594
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask.done(AsyncTask.java:309)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:464)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:125)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:82)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask.call(AsyncTask.java:295)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:  Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at libcore.io.Posix.android_getaddrinfo(Native Method)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:125) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:82) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask.call(AsyncTask.java:295) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:  Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at libcore.io.Posix.android_getaddrinfo(Native Method) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:451) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:215) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.Network.resolveInetAddresses(Network.java:29) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:125) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:82) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask.call(AsyncTask.java:295) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
10-18 18:12:05.310 2594-2711/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
10-18 18:12:05.328 1296-1846/? W/ActivityManager:   Force finishing activity com.httpkhronolog.khronolog/.Login_Activity

我知道我的问题出在 aynctask 上,但我不确定如何解决它:(。请帮助

Internet权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />