0

I am sending Json String to my webapi php file from Android Using OkHttpClient from background Service.

The Code of Web Service Is

 if (isset($_POST)) {

     $data = json_decode(file_get_contents('php://input'), true);

 }

The Android Code is :

String requestString = jsonObject.toString();

URL url = new URL("http://cbs.octa.in/webapis/InsertData.php");
OkHttpClient client = new OkHttpClient();
MediaType mdt = MediaType.parse("application/json; charset=utf-8");

RequestBody body = RequestBody.create(JSON, requestString); 
Request request = new Request.Builder()
            .url(url)
            .header("Content-Type","application/json")
            .post(body)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.v("BGAPI", "RESPONSE ERROR " + e.getMessage());
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.v("BGAPI", "RESPONSE " + response);
        }
    });

The output for this call i am getting is :

 RESPONSE ERROR Unable to resolve host "cbs.octa.in": No address associated with hostname

I tried this call from postman and its working , even i confirmed by entering the same url in broweser.

My question is to send data to server in that web api. Please guide me.

Note : For Privacy i have not given the actual domain name.

  • 1
    "ESPONSE ERROR Unable to resolve host "cbs.octa.in" Are you testing on an Android device? Is it on the same network as your browser, with access to this host? This message simply means Android can't resolve that host, it's looking up that name and getting nothing. If that IS the right hostname, is it on an intranet or something that you haven't also put the client on? Check your Android device networking settings. – Charlie Collins Mar 19 '18 at 18:53
  • I am testing it on Android Device. But the device is connected to the Internet. and the api is one the server. No Local transactions. In Postman it works but in Android Background Service it is not working. basically what i am trying to do is , when internet connection is not available store the data into Local storage and when net is connected send it to server. – JavaEnthusias Mar 19 '18 at 18:56
  • The website address or the API url that you have provided, is not hosted in the public server which is accessible from the outside world. I just pinged and got the same response as Unknown host. – Reaz Murshed Mar 19 '18 at 18:58
  • @ReazMurshed : i have written a note at the end of the question Note : For Privacy i have not given the actual domain name. – JavaEnthusias Mar 19 '18 at 19:02
  • Why you are using OkHttpClient. Instead, try this with Volley or Retrofit – AmitCharkha Mar 20 '18 at 05:49
  • @AmitCharkha : the same error i am getting for the Volley or Retrofit. – JavaEnthusias Mar 20 '18 at 06:27

0 Answers0