使用localhost的Android
我有以下网址使用localhost的Android,android,Android,我有以下网址 mvc.local/users/login 为此,我有以下Android代码: public String sendToServer(List<NameValuePair> pair, String url){ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try { httppost.setEnti
mvc.local/users/login
为此,我有以下Android代码:
public String sendToServer(List<NameValuePair> pair, String url){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse response = httpclient.execute(httppost);
if (response != null) {
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
{
try {
throw new Exception(response.getStatusLine().getReasonPhrase());
} catch (Exception e) {
e.printStackTrace();
}
}
StringBuilder sb = new StringBuilder();
String s;
while(true)
{
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
return sb.toString();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Error";
}
因此,我的问题是如何使用本地主机?您是在模拟器上还是在物理设备上尝试此操作?我使用的是模拟器可能会有所帮助:@NKN如果我使用的本地主机是您可以看到的名为mvc.local的主机怎么办使用:10.0.2.2/users/login不起作用?
Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=