0

我是数据传输到在线服务器的新手。我一直在关注它的多个教程,因为 DefaultHttpClient 在最新的 sdk 中已被弃用,所以我一直在使用 3rd 方库 Ion Github Link,我使用 000Webhost 域作为在线服务器。

我正在关注这个Android:暂时将 httpURLConnection 连接到服务器答案,

这是我的客户端 android 应用程序代码

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也不太了解。请帮忙,谢谢!!

4

1 回答 1

0

响应不是 json 对象,因此被视为错误,显然我将所有错误视为“未找到服务器”

于 2016-01-31T06:08:20.790 回答