Search code examples
phpandroidmysqlweb-hostingandroid-ion

Server not found error, 000Webhost domain and Ion library for data sending?


I am a newbie for data transfer to online sever. I have been following multiple tutorial for it, as DefaultHttpClient has been deprecated in latest sdk so i have been using a 3rd party library Ion Github Link, I am using 000Webhost domain as an online server.

I am following this Android: connect httpURLConnection to the server answer for now,

Here is my client side android app code

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;
            }
        });

The php file which i want to connect is in main(root) directory of my domain http://addressbook.netau.net.

Here is the php code

<?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();
}
?>            

What i am trying to do is to send some data from my application to online server and then using php, store that data into table i.e. mysql database. I don't know much about php as well. Please Help, Thank you!!


Solution

  • The response is not a json object, so it is treated as an error, and apparently I consider all errors as "No server found"