Android URL 连接错误:使用 XAMPP 的数据库连接错误

Posted

技术标签:

【中文标题】Android URL 连接错误:使用 XAMPP 的数据库连接错误【英文标题】:Android URL Connection error: Database connection error using XAMPP 【发布时间】:2021-03-11 06:48:31 【问题描述】:

我目前正在构建一个需要管理历史交易数据库的项目。

我按照本网站的说明将我的数据库连接到项目:

How To Connect An android App To A mysql Database

之后,我尝试在Fragment_History 中使用此代码构建我的项目,但是出现了这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mind.loginregisterapps, PID: 6912
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:121)
    at org.json.JSONTokener.nextValue(JSONTokener.java:98)
    at org.json.JSONArray.<init>(JSONArray.java:94)
    at org.json.JSONArray.<init>(JSONArray.java:110)
    at com.mind.loginregisterapps.Fragment_History.loadIntoListView(Fragment_History.java:83)
    at com.mind.loginregisterapps.Fragment_History.access$000(Fragment_History.java:24)
    at com.mind.loginregisterapps.Fragment_History$1DownloadJSON.onPostExecute(Fragment_History.java:55)
    at com.mind.loginregisterapps.Fragment_History$1DownloadJSON.onPostExecute(Fragment_History.java:42)
    at android.os.AsyncTask.finish(AsyncTask.java:771)
    at android.os.AsyncTask.access$900(AsyncTask.java:199)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我查看了代码,发现downloadJSON中的URL连接失败导致null值被传递。

因此,我添加了res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

并编辑清单:

<application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        android:usesCleartextTraffic="true"
</application>

然后,使用HTTPUrlConnection时出现此错误,我迷茫了:

Failed to connect to localhost/127.0.0.1:80

我尝试将downloadJSONnetwork_security_config.xml中的localhost改成127.0.0.1,错误变成:

Failed to connect to /127.0.0.1:80

我该如何解决这个问题?请告诉我。

非常感谢!

代码如下:

Fragment_History.java

public class Fragment_History extends Fragment 

ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_history, container, false);

    listView = (ListView)view.findViewById(R.id.list_history);
    downloadJSON("http://localhost/hist_login.php");

    return view;


private void downloadJSON(final String urlWebService) 

    class DownloadJSON extends AsyncTask<Void, Void, String> 

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


        @Override
        protected void onPostExecute(String s) 
            super.onPostExecute(s);
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            try 
                loadIntoListView(s);
             catch (JSONException e) 
                e.printStackTrace();
            
        

        @Override
        protected String doInBackground(Void... voids) 
            try 
                URL url = new URL(urlWebService);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String json;
                while ((json = bufferedReader.readLine()) != null) 
                    sb.append(json + "\n");
                
                return sb.toString().trim();
             catch (Exception e) 
                return null;
            
        
    
    DownloadJSON getJSON = new DownloadJSON();
    getJSON.execute();


private void loadIntoListView(String json) throws JSONException 
    JSONArray jsonArray = new JSONArray(json);
    ArrayList<History> list = new ArrayList<History>();
    for (int i = 0; i < jsonArray.length(); i++) 
        JSONObject obj = jsonArray.getJSONObject(i);
        list.add(new History(obj.getString("h_rName"),obj.getDouble("h_amount")));
    
    HistoryAdapter adapter = new HistoryAdapter(list, getActivity());
    listView.setAdapter(adapter);

hist_login.php

<?php
$db = "test";
$user = "root";
$pass = "";
$host = "localhost";

// Create connection
$con=mysqli_connect($host,$user,$pass,$db);
 
// Check connection
if (mysqli_connect_errno())

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

 
// Select all of our stocks from table 'history'
$sql = "SELECT * FROM history";
 
// Confirm there are results
if ($result = mysqli_query($con, $sql))

    // We have results, create an array to hold the results
        // and an array to hold the data
    $resultArray = array();
    $tempArray = array();
 
    // Loop through each result
    while($row = $result->fetch_object())
    
        // Add each result into the results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    
 
    // Encode the array to JSON and output the results
    echo json_encode($resultArray);
 else

    echo "Not connected...";

 
// Close connections
mysqli_close($con);
?>

【问题讨论】:

我的回答让你满意吗? 【参考方案1】:

是的,如果您使用的是模拟器。在 Android Studio emulator-host network Host 的 ip 是 10.0.2.2

有时我在模拟器上有时有一个真实的设备时,我在更改它时遇到了这个问题。所以我做了这个你可以使用的配置

在模块 build.gradle

android
    buildTypes 
        release 

            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String" , "BASE_URL" , "\"https://acme.co/\""

            resValue "string", "BASE_URL_SCHEME", "\"https\""
            resValue "string", "BASE_URL_HOST", "\"acme.co\""

            manifestPlaceholders = [crashlyticsEnabled: true]
        
        debug 
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ext.alwaysUpdateBuildId = false

            //debug with server url
            buildConfigField 'String' , 'BASE_URL' , "\"https://acme.co/\""

            resValue "string", "BASE_URL_SCHEME", "\"https\""
            resValue "string", "BASE_URL_HOST", "\"acme.co\""

            ext.enableCrashlytics = false
            manifestPlaceholders = [crashlyticsEnabled: false]
        

        debug_emulator

            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ext.alwaysUpdateBuildId = false

            //"http://10.0.2.2/tak/public/"
            buildConfigField 'String' , 'BASE_URL' , "\"http://10.0.2.2/\""

            resValue "string", "BASE_URL_SCHEME", "\"http\""
            resValue "string", "BASE_URL_HOST", "\"10.0.2.2\""

            ext.enableCrashlytics = false
            manifestPlaceholders = [crashlyticsEnabled: false]
        

        debug_physical

            debuggable true
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            ext.alwaysUpdateBuildId = false

            //"http://192.168.43.100/tak/public/"
            buildConfigField 'String' , 'BASE_URL' , "\"http://192.168.43.100/\""

            resValue "string", "BASE_URL_SCHEME", "\"http\""
            resValue "string", "BASE_URL_HOST", "\"192.168.43.100\""

            ext.enableCrashlytics = false
            manifestPlaceholders = [crashlyticsEnabled: false]
        
    

所以我在这里有 4 个 url 配置如下:

release 用于应用程序发布 debug 用于调试和使用服务器 ip/domain 示例:acme.co debug_emulator 用于在我使用 Android Studio 模拟器并且服务器位于我的本地主机时进行调试 (您的情况) debug_physical 当我在与本地服务器位于同一专用网络中的真实设备上进行调试时。

我在 Android 代码中使用 url,如下所示:

public static final String BASE_URL = BuildConfig.BASE_URL;

当我想编译时,我要进行的唯一切换就是这个 buildType。这些设置将插入正确的 url。

在你从下拉菜单编译之前改变它:

【讨论】:

【参考方案2】:

我自己设法解决了。我所做的是将http更改为:

http://10.0.2.2/hist_login.php

因为它是 Android Studi 上模拟器的领域。

【讨论】:

以上是关于Android URL 连接错误:使用 XAMPP 的数据库连接错误的主要内容,如果未能解决你的问题,请参考以下文章

Android - 尝试连接到本地服务器时出错(Xampp)

Android 到 XAMPP 的连接被拒绝

如何将android与php mysql连接[重复]

localhost 拒绝连接 Xampp 问题

Android 设备和 xampp 服务器连接

在notepad++中使用php文件连接XAMPP SQL