gpt4 book ai didi

java - 在 Android Studio 中将数据从 MySQL 显示到 TextView

转载 作者:行者123 更新时间:2023-11-30 22:11:52 25 4
gpt4 key购买 nike

我想使用 TextView 将 MySQL 中的数据显示到 Android 中,但是当我运行该应用程序时,它会强制自行停止。

谁能帮我解决一下?任何想法都会很好。

dbconfig.php:

<?php

//This script is designed by Android-Examples.com
//Define your host here.
$servername = "localhost";
//Define your database username here.
$username = "root";
//Define your database password here.
$password = "";
//Define your database name here.
$dbname = "u727224026_demo";

?>

send_data.php

<?php
include 'dbconfig.php';


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM TextViewTable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row
while($row[] = $result->fetch_assoc()) {

$json = json_encode($row);


}
} else {
echo "0 results";
}
echo $json;
$conn->close();
?>

MainActivity.java

package flix.yudi.kuesioner2;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView textview;
JSONObject json = null;
String str = "";
HttpResponse response;
Context context;
ProgressBar progressbar;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressbar = (ProgressBar)findViewById(R.id.progressBar1);
textview = (TextView)findViewById(R.id.textView1);
button = (Button)findViewById(R.id.button1);

progressbar.setVisibility(View.GONE);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

progressbar.setVisibility(View.VISIBLE);

new GetTextViewData(context).execute();

}
});
}


private class GetTextViewData extends AsyncTask<Void, Void, Void>
{
public Context context;


public GetTextViewData(Context context)
{
this.context = context;
}

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

@Override
protected Void doInBackground(Void... arg0)
{

HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost("http://192.168.100.6/a_test/send-data.php");

try {
response = myClient.execute(myConnection);
str = EntityUtils.toString(response.getEntity(), "UTF-8");

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


try{
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0);



} catch ( JSONException e) {
e.printStackTrace();
}

catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result)
{
try {
textview.setText(json.getString("ServerData"));

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//Hiding progress bar after done loading TextView.
progressbar.setVisibility(View.GONE);

}
}


}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jsonparsingfromurltextview_android_examples.com.MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView Static Text Before Load"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"/>

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:text="Click Here to Load TextView data dynamically from MySQL Database Online Using JSON Parsing" />

logcat 错误通知

10-04 15:41:44.516 22210-22210/flix.yudi.kuesioner2 E/AndroidRuntime: FATAL EXCEPTION: main Process: flix.yudi.kuesioner2, PID: 22210 java.lang.NullPointerException at flix.yudi.kuesioner2.MainActivity$GetTextViewData.onPostExecute(MainActivity.java:114) at flix.yudi.kuesioner2.MainActivity$GetTextViewData.onPostExecute(MainActivity.java:60) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5593) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method)

我试图找到修复它的方法,但还是一无所获,请帮助我。

来自 HTTP_REQUESTER 的附加信息响应:

enter image description here

最佳答案

问题好像出在新这一行

new GetTextViewData(context).execute();

Context 是一个指针,但没有引用任何内容...这会导致空指针异常。

在传递上下文时使用:

new GetTextViewData(this).execute();

希望这对您有所帮助。

关于java - 在 Android Studio 中将数据从 MySQL 显示到 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847872/

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