我有一个应用程序,非常简单。在准备字典时,应该输入单词并将其输入数据库。输入部分由php脚本完成,效果很好。产生的链接也有效,我手动尝试过。我想问一下,我在这段代码中做了什么监督,因为我在Google上没有找到任何有关该主题的信息:
private class SendData extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(uri.toString());
EditText text = (EditText) findViewById(R.id.oshikwanyamaEx);
text.setText(url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.connect();
}catch (MalformedURLException m){
m.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (urlConnection != null){
urlConnection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(MainActivity.this, "Data was sent", Toast.LENGTH_LONG).show();
super.onPostExecute(aVoid);
}
}
先谢谢您 这是PHP代码:
<?php
//Creating a connection
$con = mysqli_connect("breidinga.lima-db.de:3306","USER373834","*********","db_373834_1");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL, '', '', '', NULL, NULL, NULL, NULL)
$sql = "INSERT INTO `ENGOKY` (`_id`, `eng`, `oky`, `type`, `engex`, `okyex`, `okypl`, `engpl`) VALUES (NULL,'".$_GET["eng"]."', '";
//$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', '".$_GET["engex"]."', '".$_GET["okyex"]."', '".$_GET["okyex"]."', '".$_GET["ekypl"]."', '".$_GET["engpl"]."')";
$sql = $sql.$_GET["oky"]."', '".$_GET["type"]."', NULL, NULL, NULL, NULL)";
$sql = str_replace("''","NULL", $sql);
mysqli_query($con ,$sql);
mysqli_free_result($result);
mysqli_close($con);
?>
答案 0 :(得分:1)
您的代码有一些改进,还有一些声明
URL url = new URL("your URL here");
JSONObject postDataParams = new JSONObject();
postDataParams.put("word", "onomatopoeia"); //Change #1
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000); //Change #2
conn.setConnectTimeout(15000); //Change #2
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
1-您需要将参数传递到您的PHP文件中,因为我看不到任何证据。 假设变量类似于以下内容:-
$word= $_POST['word'];
2-设置连接超时和读取超时总是有用的,因为这将在一段时间后终止请求。在这种情况下,这是15秒。
3-作为指导,仅需确保PHP文件有返回响应即可,以确保插入是否成功。
4-添加以下权限,可能会有所不同
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5-从连接中读取响应代码,这将确定它是否在做任何事情。
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "Response code is: " + response);
if ((response >= 200) && (response < 300)) {
Log.d(DEBUG_TAG, "The response is: " + conn.getResponseMessage());
}
6。用户收到响应代码301(重定向)并添加此行以停止重定向
conn.setInstanceFollowRedirects(false);