0

I have created a simple ASP.NET MVC(5) web site with following Action in HomeController:

[HttpPost]
    public ActionResult ReadJson()
    {
        var model = new { Name = "Ivan", Age = 20 };

        return new JsonResult { Data = model, JsonRequestBehavior = JsonRequestBehavior.DenyGet };
    }

I am trying to get the Json result from Android device, so I've created test project, and added class which extends AsyncTask. Here is overrided doInBackground method:

@Override
protected Boolean doInBackground(String... params) {
    String url_select = "http://10.0.2.2:63938/Home/ReadJson";
    ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

    try {
        HttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url_select);
        httpPost.setEntity(new UrlEncodedFormEntity(param));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        inputStream = httpEntity.getContent();

    } catch (UnsupportedEncodingException e1) {
        Log.e("UnsupportedEncodingException", e1.toString());
        e1.printStackTrace();
    } catch (ClientProtocolException e2) {
        Log.e("ClientProtocolException", e2.toString());
        e2.printStackTrace();
    } catch (IllegalStateException e3) {
        Log.e("IllegalStateException", e3.toString());
        e3.printStackTrace();
    } catch (IOException e4) {
        Log.e("IOException", e4.toString());
        e4.printStackTrace();
    }
    // Convert response to string using String Builder
    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream), 8);
        StringBuilder sBuilder = new StringBuilder();

        String line = null;
        while ((line = bReader.readLine()) != null) {
            sBuilder.append(line + "\n");
        }

        inputStream.close();
        result = sBuilder.toString();

        //Shows Html markup with: BAD REQUEST - Invalid Hostname
        Log.d("FINISH: ", result);

    } catch (Exception e) {
        Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
    }

    return true;
}

I am logging the result and it writes HTML which tells that the Hostname is invalid. I have tried with 10.0.2.2:63938 and with 192.168.0.103:63938 (pc's local IP), where the 63938 is port generated from IIS. I can access Action method from browser, but not from Android virtual device, any idea?

Amal Dev
  • 1,938
  • 1
  • 14
  • 26
M.Veli
  • 519
  • 1
  • 6
  • 15
  • You are using local server for fetching data??? – Pankaj May 17 '15 at 11:49
  • use postman or advance rest client tool addons for chrome browser to check local server service – Pankaj May 17 '15 at 11:51
  • For test purposes the client(android) and server(asp.net mvc web site) are running in the same machine. – M.Veli May 17 '15 at 11:53
  • Have you ever use postman or advance rest client for checking service. first check if the service is working in postman or in advance rest client. – Pankaj May 17 '15 at 11:55
  • I can access site from the browser... – M.Veli May 17 '15 at 11:58
  • Can you access your website / JSON from the browser _on_ the Android virtual device ? Do you have internet permission for your app ? Try crating a new AVD as per [here](http://stackoverflow.com/a/9777798/4428462). – Jonas Czech May 17 '15 at 12:54
  • `I am logging the result and it writes HTML which tells that the Hostname is invalid`. Please show exacttly where and how you are doing that. – greenapps May 17 '15 at 19:34
  • `to get the Json result from Android device,`. So a real device. Not an emulator. Then using the ip of your pc should do. But a firewall might pevent it. Switch it off for a test. – greenapps May 17 '15 at 19:36
  • I have never been able to hit a local web service from a device, only an emulator. I'm interested to see if you find a solution. – nasch May 18 '15 at 00:11
  • I have tried with PC's local IP, also with 10.0.2.2 but it says that the hostname is invalid, in the code of **doInBackground** method I am logging the **result** from request it is in the second try/catch block, and the **result** is a private member of the class which overrides AsyncTask. – M.Veli May 18 '15 at 18:18
  • Here is the error upon logging the **result**: 'Bad Request' with body: '

    Bad Request - Invalid Hostname


    HTTP Error 400. The request hostname is invalid.

    ' I have set the internet permission in the manifest.
    – M.Veli May 18 '15 at 18:34
  • I can't connect to the web site from AVD's web browser it shows the same error page: *Bad request HTTP Error 400: The host name is invalid* – M.Veli May 18 '15 at 18:42

0 Answers0