Android Question USB Cable Connection - Consume Service Json (localhost)

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I have a local web service in php that returns a Json, I want to consume this local web service from B4A using a USB connection from the computer to the mobile phone (using wifi works perfectly)

When I compile the application in B4A it runs perfectly on the mobile phone, but when I consume the local web service it does not work using the USB connection.

Is there any way to do it with a USB connection?

Thank you
 

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
You're right, @Erel , that's the only way.

When I do a project in B4A to add 2 numbers, the application is compiled and executed in mobile phone without any problem by the USB connection.

The problem is when I try to consume a Local Web Service that was made in PHP and that returns a JSON,the code in PHP es:

PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$id=$_REQUEST["id"];

$sql  = "SELECT password from usuarios where id=".$id;
try
{
    $conn = new PDO("mysql:host=$servername;dbname=prueba", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmn = $conn->prepare($sql);
    $stmn->execute();
    $data = $stmn->fetchAll(PDO::FETCH_ASSOC);
    $myJSON = json_encode($data,128);
    echo $myJSON;
    }

catch(PDOException $e)
    {
    //conexion fallida - retorna 0
    $myJSON = json_encode(0,128);
    echo $myJSON;
    }

Consume Service from the browser:

upload_2018-8-22_10-17-7.png


Code in B4A to consume the Local Web Service:

B4X:
Sub GetPassUser(Val As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("http://localhost/WSAndroid/wsclientes.php?id="&Val)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As List = parser.NextArray
        If root.Size>0 Then
            For Each colroot As Map In root
                Dim password As String = colroot.Get("password")
            Next
            If TxtPassword.Text=password Then
                Msgbox("Password Correct","Message!!!")
            Else
                Msgbox("Password Incorrect","Error!!!")
            End If
        Else
            Msgbox("User NO Exist","Error!!!")
        End If
    End If
    j.Release
End Sub

the error exits when you connect to the local web service

upload_2018-8-22_10-27-22.png


The error is as follows:

org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused

Thanks.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You need to use the IP address of your PC, not localhost, not 127.0.0.1 (localhost=127.0.0.1). Look at the link I posted for more information.
 
Upvote 0
Top