我想从主机上的虚拟 android 设备发出 HTTP post 请求。 您将在下面看到我如何使用旧的 WebFetch 工具发布的图像。
- 我不知道使用什么 URL 来调用主机?
- 我不知道我的正文字符串如何用作输入?
有人知道如何解决这个问题吗?
最佳答案
如果你想连接到运行Android模拟器的电脑,使用IP地址10.0.2.2。您可以阅读更多相关信息 here .
另请查看以下问题中已接受的答案,以了解如何将 json 作为发布数据发送:
How to send POST request in JSON using HTTPClient?
您可以使用以下代码来发起 HTTP get 请求:
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://10.0.2.2:port/your_path_with_parameter";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
关于java - Android http post 高级请求与主机上的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12981419/