0

I created an api using cakephp and I was trying to test it on genymotion or on an android device but every time I got :

W/System.err: java.net.ConnectException: failed to connect to /192.168.xxx.xxx (port 8765): connect failed: ECONNREFUSED (Connection refused)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at java.net.Socket.connect(Socket.java:882)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:148)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.xxx.TaskManager.doInBackground(TaskManager.java:41)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at com.xxx.TaskManager.doInBackground(TaskManager.java:21)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 10:22:58.142 6362-6567/com.xxx W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at java.lang.Thread.run(Thread.java:818)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err: Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at libcore.io.Posix.connect(Native Method)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
07-21 10:22:58.143 6362-6567/com.xxx W/System.err:  ... 18 more

with genymotion I tried to put 10.0.2.2 or 10.0.3.2 it didn't work

with my android device I put my ip address but it didn't work too

this is my android code :

public class TaskManager extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        String result ;
        String url = "http://192.168.1.18:8765/backend/0/map/test";
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImV4cCI6MTUwMTEzNDc4OX0.-gKf16-MPncflfuqPI6O_qg_60RFBVlPAy8nO3MaVAM";
        String location_id = "2";
        URL object = null;
        try {
            object = new URL(url);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Authorization", token);
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");

            JSONObject cred = new JSONObject();
            cred.put("location_id", location_id);
            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
            wr.write(cred.toString());
            wr.flush();
            StringBuilder sb = new StringBuilder();
            int HttpResult = con.getResponseCode();
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                JSONObject jObj = new JSONObject(sb.toString());
                result = jObj.getString("results");
                Log.e("result",result+"result");
                if (result != null) {
                    return result;
                }
            } else {
                System.out.println(con.getResponseMessage());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.e("result","null "+result);
    }
}

I disabled my firewall (I'm using a mac) and I test it on postman with localhost and it works fine ! any suggestion how to make it work on the device or genymotion ?

1 Answers1

0

you are doing wrong here mate

change this
http://localhost:8765/backend/0/map/test

to your Current Ip
like 192.168.**********

this might work.you cannot use localhost,you have to provide specific ip.

seon
  • 1,050
  • 2
  • 13
  • 27
  • I used http://localhost:8765/backend/0/map/test to test on postman , but I changed to mu current ip address to test on the android device it didn't work . and I changed to 10.0.2.2 and 10.0.3.2 for genymotion it didn't work also –  Jul 21 '17 at 02:07
  • I even tried to change the port but it still doesn't work –  Jul 21 '17 at 02:11
  • take a look at this https://stackoverflow.com/questions/28167054/java-net-connectexception-failed-to-connect-to-192-168-253-3-port-2468-conn. check is the server and Client on same network or not – seon Jul 21 '17 at 03:11
  • I tired but I think the problem may be from mamp actually . any helps on that ? –  Jul 21 '17 at 04:03
  • the problem is sure on simple error ? while working have you checkedd both device connect on same network. – seon Jul 21 '17 at 04:14
  • I'm using mamp to start apach server and get access to phpmyadmin –  Jul 21 '17 at 04:16