我正在尝试从我的Android应用程序连接到我的vagrant服务器。当我运行此代码时:
try {
//Build the connection
URL url = new URL("http://10.0.2.2:8000/test");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setDoInput(true);
connection.setDoOutput(true);
HashMap<String, String> postDataParams = new HashMap<String, String>();
postDataParams.put("param1", "2");
postDataParams.put("param2", "4567");
postDataParams.put("param3");
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
connection.connect();
Integer responsecode = connection.getResponseCode();
String message = "The Response Was: " + Integer.toString(responsecode);
Log.d("MainActivity", message);
Log.d("MainActivity", connection.getResponseMessage());
//Is dit nodig???
//con.setRequestProperty("User-Agent", USER_AGENT);
} catch (IOException e) {
String msg = (e.getMessage()==null)?"Geen message!":e.getMessage();
Log.i("Error",msg);
}
}
我得到404
作为回复。但是,当我从我的模拟器启动浏览器并转到我得到我想要的页面时。
我怎么解决这个问题?