Android主机名不能为空
我知道这个问题已经被问过很多次了,但是这些问题的答案对我来说并不适用 我正在尝试访问REST API,但当我尝试连接时,出现以下错误:Android主机名不能为空,android,rest,httpclient,Android,Rest,Httpclient,我知道这个问题已经被问过很多次了,但是这些问题的答案对我来说并不适用 我正在尝试访问REST API,但当我尝试连接时,出现以下错误: java.lang.IllegalArgumentException:主机名不能为null 我的URL是“http:localhost:3000/api/v1/users” 我的代码: public String post(List<NameValuePair> data) { String result = null; HttpPo
java.lang.IllegalArgumentException:主机名不能为null
我的URL是“http:localhost:3000/api/v1/users”
我的代码:
public String post(List<NameValuePair> data) {
String result = null;
HttpPost request;
try {
request = new HttpPost("http:localhost:3000/api/v1/users");
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = client.execute(request, handler);
client.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return result;
}
公共字符串post(列表数据){
字符串结果=null;
HttpPost请求;
试一试{
request=newhttppost(“http:localhost:3000/api/v1/users”);
ResponseHandler=new BasicResponseHandler();
试一试{
结果=client.execute(请求、处理程序);
client.getConnectionManager().shutdown();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}捕获(不支持DencodingException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
返回结果;
}
在第一个视图中,我认为URL的传递存在一些格式问题
- 将url从更改为
http:localhost:3000/api/v1/users
到
编辑
And yes if you are running the emulator it is already taken the local host
提供更多信息。
android
不理解localhost
。因此,请将localhost
替换为10.0.2.2
,然后重试。它应该会起作用
试试这个
http://10.0.2.2:3000/api/v1/users
试试这个
http://10.0.2.2:3000/api/v1/users
Android不了解本地主机行pc浏览器 您是否在浏览器http:localhost:3000/api/v1/users中检查这一点?
http://10.0.2.2:3000/api/v1/users