gpt4 book ai didi

java - Android 应用程序无法将数据插入 MySQL DB

转载 作者:行者123 更新时间:2023-11-29 21:38:08 25 4
gpt4 key购买 nike

我对 Android 编码相当陌生,我正在尝试创建将一些信息插入我的 MySQL 数据库的应用程序。我在网上找到了很多教程和技巧,并创建了精简版应用程序来尝试程序。一切编译正常,应用程序运行,并且似乎已成功发送数据。但事实上,我的表中没有出现任何数据。
这是我的 PHP 代码 android_add.php:

<?php
$con = mysqli_connect(localhost, user, psswd, name); //those works
mysqli_set_charset($con, "utf8"); //working with special symbols

$name = $_POST['name']; //get name & author from App
$author = $_POST['author'];

$sql = "insert into kniha_test (k_autor_pr,k_nazev) values ('$name','$address')";
if(mysqli_query($con,$sql)){
echo 'success';
} else {
echo 'failure';
}
mysqli_close($con);
?>

这是我的 MainActivity.java:

import android.content.ContentValues;
import android.os.AsyncTask;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private EditText editTextName;
private EditText editTextAuthor;

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

editTextName = (EditText) findViewById(R.id.editTextName);
editTextAuthor = (EditText) findViewById(R.id.editTextAuthor);
}

public void insert (View view){
String name = editTextName.getText().toString();
String author = editTextAuthor.getText().toString();

insertToDatabase(name,author);
}

protected void insertToDatabase(String name, String author){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {


String name;
String author;
public void saveNameAut(String name, String author){
this.name = name;
this.author = author;
name = editTextName.getText().toString();
author = editTextAuthor.getText().toString();
}

@Override
protected String doInBackground(String... params){
String paramName = params[0];
String paramAuthor = params[1];



ContentValues values = new ContentValues();
values.put("name", this.name);
values.put("author", this.author);

String addUrl = "http://glaserproject.com/knihovna_kyber/android/android_add.php";

try {URL url = new URL(addUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

System.out.println("Response Code: " + conn.getResponseCode());

} catch (IOException e){};

return "Succes";
}

@Override
protected void onPostExecute(String result){
super.onPostExecute(result);

Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
textViewResult.setText("inserted");
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, author);
}

}

正如我所说,我只是初学者,所以可能有一些非常愚蠢的事情,但我不知道是什么。并且可能会有一些来自不同尝试的垃圾线。 PHP 代码应该没问题,我使用几乎相同的方法从 HTML 插入,所以我猜测,我的 Java 代码有问题。
我将非常感谢您的建议/回复。
谢谢!

PS:我收到的响应代码是 200。

最佳答案

您正在通过 AsyncTask 发送空值您是否打印了要发送的值
试试这个

protected void insertToDatabase(String name, String author){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {


String cName=name;
String cAuthor=author;


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

ContentValues values = new ContentValues();
values.put("name", cName);
values.put("author", cAuthor);

String addUrl = "http://glaserproject.com/knihovna_kyber/android/android_add.php";

try {URL url = new URL(addUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");

System.out.println("Response Code: " + conn.getResponseCode());

} catch (IOException e){};

return "Succes";
}

@Override
protected void onPostExecute(String result){
super.onPostExecute(result);

Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
textViewResult.setText("inserted");
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, author);
}

再试一次,让我知道它是否解决了您的问题....

关于java - Android 应用程序无法将数据插入 MySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34751599/

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