gpt4 book ai didi

java - 如何将 Android 应用程序连接到 MySQL 数据库?

转载 作者:行者123 更新时间:2023-11-29 12:05:06 25 4
gpt4 key购买 nike

我对 Android 应用程序编程非常陌生,所以请帮助我。我想将我的应用程序连接到 MySQL 数据库。

但是我收到一个导致应用程序崩溃的错误。这是 LogCat 中的错误:

07-26 12:32:47.785: E/AndroidRuntime(276): FATAL EXCEPTION: main
07-26 12:32:47.785: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.massar_parent/com.example.parent.MainActivity}: java.lang.NullPointerException
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 12:32:47.785: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 12:32:47.785: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
07-26 12:32:47.785: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 12:32:47.785: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-26 12:32:47.785: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
07-26 12:32:47.785: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
07-26 12:32:47.785: E/AndroidRuntime(276): at com.example.massar_parent.MainActivity.onCreate(MainActivity.java:44)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 12:32:47.785: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

以及MainActivity.java中的代码

import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
//private TextView texte = null;
TextView txt;

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


txt.setText("Connexion...");


// Appeler la méthode pour récupérer les données JSON
txt.setText(getServerData(strURL));
}

// Mettre l'adresse du script PHP
// Attention localhost ou 127.0.0.1 ne fonctionnent pas. Mettre l'adresse IP
// local.
public static final String strURL = "http://localhost/massar/connection.php";

private String getServerData(String returnString) {
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("NomEleve", "L"));

// Envoie de la commande http
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convertion de la requête en string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// Parse les données JSON
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
// Affichage ID_ville et Nom_ville dans le LogCat
Log.i("log_tag", "CNE: " + json_data.getInt("CNE") + ", NomEleve: "
+ json_data.getString("NomEleve"));
// Résultats de la requête
returnString += "\n\t" + jArray.getJSONObject(i);
}

} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return returnString;
}
}

这是代码 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.massar_parent"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

希望有人能帮忙:/

最佳答案

我发现有两个错误:

  1. TextView 未初始化。你知道findViewById()吗?
  2. 您直接在主 UI 线程上进行网络调用,这会导致问题!

关于java - 如何将 Android 应用程序连接到 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com