Php 如何将数据从android发送到mysql?
我想从android发送数据到php文件,该文件正在使用POST方法将数据添加到mysql数据库中,我不知道我是如何做到这一点的。。有什么帮助吗 我已经通过方法post连接了http。。不知道现在发生了什么Php 如何将数据从android发送到mysql?,php,android,mysql,Php,Android,Mysql,我想从android发送数据到php文件,该文件正在使用POST方法将数据添加到mysql数据库中,我不知道我是如何做到这一点的。。有什么帮助吗 我已经通过方法post连接了http。。不知道现在发生了什么 public URL makeUrl(String stringUrl) { URL url = null; try { url = new URL(stringUrl); } catch (MalformedURLE
public URL makeUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Problem building the URL ", e);
}
return url;
}
public static void HttpConnect(URL url) {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("POST");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
//to do
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
用POST方法添加的PHP文件
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
require 'connect.php';
create();
}
function create(){
global $connect;
$name = $_POST["nazwa"];
$ingredients = $_POST["skladniki"];
$price = $_POST["cena"];
$type = $_POST["typ"];
$photo = $_POST["zdjecie"];
$query = "INSERT INTO dania('nazwa','skladniki','cena','typ','zdjecie') VALUES ('$name','$ingredients','$price','$type','$photo');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
使用Uri.Builder附加POST有效负载并将其写入OutputStream 在下面的代码中,将您的_值1、_值2、_值3、_值4、_值5替换为您希望发布到服务器的合适值 我还建议将这段代码包装在AsyncTask中 例如
对于完整的示例,我已经在我的中写了关于AsyncTask的内容。这是临时的,我想先添加come内容,以检查所有内容是否一起工作。
public static void HttpConnect(URL url) {
StringBuilder stringBuilder = new StringBuilder();
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("nazwa",YOUR_VALUE1);
builder.appendQueryParameter("skladniki",YOUR_VALUE2);
builder.appendQueryParameter("cena",YOUR_VALUE3);
builder.appendQueryParameter("typ",YOUR_VALUE4);
builder.appendQueryParameter("zdjecie",YOUR_VALUE5);
String urlQuery = builder.build().getEncodedQuery();
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTf-8"));
bufferedWriter.write(urlQuery);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String line = "";
while ((line = bufferedReader.readLine()) != null){
stringBuilder.append(line).append("\n");
}
// your json response output is here
Log.d("RESULT",stringBuilder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}