android - 无法通过php脚本将数据插入到android中的服务器上

时间:2016-12-13 12:37:09

标签: php android

我想在android中使用PHP脚本向服务器插入数据,但是当我点击button进行插入时,我在logcat中出错了。

我做错了吗?

我得到D/output lines: {"success":0} ; I/System.out: success=0的相同输出,因为当点击按钮时,它会循环5次。

以下使用的所有代码:

logcat error :

D/output lines: {"success":0}
I/System.out: success=0
D/output lines: {"success":0}
I/System.out: success=0
D/output lines: {"success":0}
I/System.out: success=0
D/output lines: {"success":0}
I/System.out: success=0
D/output lines: {"success":0}
I/System.out: success=0

insert2.php

<?php
include 'dbconfig.php';
$con = new mysqli($servername, $username, $password, $dbname);
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
$id=$_POST["id"];
$nilai=$_POST["nilai"];

$success=0;
$sql = "INSERT INTO  nilai_3 (id, nilai) 
VALUES ('$id','$nilai)";

if(mysqli_query($con, $sql))
{
$success=1;
}
$response["success"]=$success;
die(json_encode($response));
mysql_close($con);
?>

button onClick

int size = adapter3.getCount();
StringBuilder b = new StringBuilder();
JSONArray jArray = new JSONArray();
for (int i=0;i<size;i++){
    id = String.valueOf(adapter3.getItem(i));
    nilai = String.valueOf(adapter3.getItem(i).getRatingStar());
    new PostDataTOServer().execute();
}

1 个答案:

答案 0 :(得分:0)

您需要将这些标题放在php api中

header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');