0

オンラインサーバーへのデータ転送初心者です。DefaultHttpClient は最新の SDK で廃止されたため、複数のチュートリアルに従っています。そのため、サードパーティのライブラリ 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