【发布时间】:2015-12-29 19:46:47
【问题描述】:
我是数据传输到在线服务器的新手。我一直在关注它的多个教程,因为 DefaultHttpClient 在最新的 sdk 中已被弃用,所以我一直在使用 3rd 方库 Ion Github Link,我使用 000Webhost 域作为在线服务器。
我现在正在关注这个Android: connect httpURLConnection to the server 的答案,
这是我的客户端安卓应用代码
row.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(ab,"Long clicked",Toast.LENGTH_LONG).show();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("AddressType", a.get(position).getKEY_Address_Type());
jsonObject.addProperty("Email", a.get(position).getKEY_Place_Email());
jsonObject.addProperty("Name", a.get(position).getKEY_Place_Name());
jsonObject.addProperty("Number", a.get(position).getKEY_Place_Number());
jsonObject.addProperty("Type", a.get(position).getKEY_Place_Type());
if(a.get(position) instanceof AutoClass){
AutoClass ab=(AutoClass)a.get(position);
jsonObject.addProperty("HintAddress", ab.getAuto_KEY_Place_Hint_Address());
jsonObject.addProperty("PlaceLangitude", ab.getAuto_KEY_Place_Langitude());
jsonObject.addProperty("PlaceLatitude", ab.getAuto_KEY_Place_Latitude());
}
else {
ManualClass ab=(ManualClass)a.get(position);
jsonObject.addProperty("PlaceAddress", ab.getKEY_Place_Address());
}
Ion.with(ab).load("http://addressbook.netau.net/config.php").setJsonObjectBody(jsonObject).asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (result != null) {
Boolean risultato = result.get("result").getAsString().equals("1");
Log.d("Result Back: ", risultato.toString());
if(risultato)
Toast.makeText(ab, "Server Added Success", Toast.LENGTH_LONG).show();
else
Toast.makeText(ab, "Server Reached Error", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(ab, "No server found", Toast.LENGTH_LONG).show();
}
});
return false;
}
});
我要连接的 php 文件位于我的域 http://addressbook.netau.net 的主(根)目录中。
这是php代码
<?php
$json = json_decode(file_get_contents('php://input'), true);
$conn = new PDO("mysql:host=mysql7.000webhost.com;dbname=a1757422_AppData","username","password");
$addType=$json['AddressType'];
$email=$json['Email'];
$name=$json['Name'];
$number=$json['Number'];
$type=$json['Type'];
if($json['PlaceLangitude']){
$latitudine = $json['PlaceLatitude'];
$longitudine = $json['PlaceLangitude'];
$hint = $json['HintAddress'];
$sql = "INSERT INTO auto(name, number, placetype, addresstype, langitude,latitude,placehint,email)
VALUES (:name, :number, :type, :addType, :longitudine, :latitudine, :hint,:email)";
$query = $conn->prepare($sql);
$query->bindParam(':name', $name, PDO::PARAM_STR);
$query->bindParam(':number', $number, PDO::PARAM_STR);
$query->bindParam(':placetype', $type, PDO::PARAM_STR);
$query->bindParam(':addresstype', $addType, PDO::PARAM_STR);
$query->bindParam(':langitude', $longitudine , PDO::PARAM_STR);
$query->bindParam(':latitude', $latitudine , PDO::PARAM_STR);
$query->bindParam(':placehint', $hint , PDO::PARAM_STR);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$result = $query->execute();
if($result)
echo json_encode(array('result' => "1"));
else
echo $query->errorCode();
}
else{
$pAddress= $json['PlaceAddress'];
$sql = "INSERT INTO manual(name, number, email, addresstype, placetype,address)
VALUES (:name, :number, :type, :email, :addType, :type, :pAddress)";
$query = $conn->prepare($sql);
$query->bindParam(':name', $name, PDO::PARAM_STR);
$query->bindParam(':number', $number, PDO::PARAM_STR);
$query->bindParam(':email', $email, PDO::PARAM_STR);
$query->bindParam(':addresstype', $addType, PDO::PARAM_STR);
$query->bindParam(':placetype', $type, PDO::PARAM_STR);
$query->bindParam(':address', $pAddress, PDO::PARAM_STR);
$result = $query->execute();
if($result)
echo json_encode(array('result' => "1"));
else
echo $query->errorCode();
}
?>
我想要做的是将一些数据从我的应用程序发送到在线服务器,然后使用 php,将这些数据存储到表中,即 mysql 数据库中。我对php也不太了解。 请帮忙,谢谢!!
【问题讨论】:
-
你的域名好像不存在,和你用的是哪个http客户端无关
-
是的,domian 不存在 addressbook.netau.net 这是真正的域
-
但即使这样也行不通
-
您可能需要修复
config.php-Host '31.170.160.129' is not allowed to connect to this MySQL server中的错误。 -
响应不是 json 对象,所以它被视为错误,显然您将所有错误视为
"No server found"
标签: php android mysql web-hosting android-ion