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?
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