user2993328
user2993328

Reputation: 103

JSON Request from Android not executing

I am having some trouble retrieving coordinates from my xampp sql server. When I type in the URL (http://192.168.1.100/cwdb/get_all_coordinates.php) on my phone, I get this list of JSON coordinates:

{"coordinates":[{"cid":"1","latitude":"36.544670","longitude":"36.544651"},{"cid":"2","latitude":"36.544270","longitude":"15.524650"},{"cid":"3","latitude":"37.544270","longitude":"16.524651"},{"cid":"4","latitude":"27.544270","longitude":"76.524651"},{"cid":"5","latitude":"27.523569","longitude":"31.146650"}],"success":1}

However when I try retrieve this through my android application I am running into a problem downloading the JSON string.

I have included the internet permission in my manifest file. Here is my code that calls the http connection class. The system.out.print prints "DATAnull"

private class JSONCoordinateTask extends AsyncTask<String, Void, ArrayList<Coordinate>> {

@Override
protected ArrayList<Coordinate> doInBackground(String... params) {
    ArrayList<Coordinate> cList = new ArrayList<Coordinate>();
    String data =  (new CoordinateHttpClient()).getCoordinateData();
    System.out.println("DATA"+data);
    try {
        cList = JSONParser.getCoordinate(data);
    } catch (JSONException e) {             
        e.printStackTrace();
    }
    return cList;
}

And here is the http client class. The system out print here does not print out at all. So my guess is that it is not executing at all for some reason?

public class CoordinateHttpClient {

    private static String BASE_URL = "http://192.168.1.100/cwdb/get_all_coordinates.php";

    public String getCoordinateData() {

        try {
            URL oracle = new URL(BASE_URL);
            BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
            System.out.println("This doesn't print out");

            StringBuffer buffer = new StringBuffer();
            String line = null;
            while (  (line = in.readLine()) != null )
                buffer.append(line + "\r\n");

            in.close();
            return buffer.toString();
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
        return null;
    }
}

Any help at all would be great.

EDIT: LOGCAT

02-23 15:35:47.105: E/TAG(29364): Error
java.net.ConnectException: failed to connect to /192.168.1.100 (port 80): connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
at java.net.URL.openStream(URL.java:462)
at com.example.citywalks.CoordinateHttpClient.getCoordinateData(CoordinateHttpClient.java:17)
at com.example.citywalks.MainActivity$JSONCoordinateTask.doInBackground(MainActivity.java:43)
at com.example.citywalks.MainActivity$JSONCoordinateTask.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
02-23 15:35:47.105: E/TAG(29364):   ... 25 more

Upvotes: 0

Views: 467

Answers (1)

PaF
PaF

Reputation: 3477

Assuming you've set up your permissions correctly (note that the uses-permission tags must be under manifest, not under application), then the only explanation is that your phone is not connected to the same network where your server resides. Since it's an IP of a local network, the only way your phone can access it is if it's connected to the same network, e.g. via wifi.

I recommend you verify that address is reachable from the phone before running your app by opening a browser on the phone, and typing in the address (http://192.168.1.100/cwdb/get_all_coordinates.php).

Upvotes: 2

Related Questions