Android 一切正常,但我';我得到了java.net.ConnectException
实际上,我在这里发布了太多关于连接网站的问题。我很抱歉,伙计们 下面是myjson.javaAndroid 一切正常,但我';我得到了java.net.ConnectException,android,android-asynctask,Android,Android Asynctask,实际上,我在这里发布了太多关于连接网站的问题。我很抱歉,伙计们 下面是myjson.java public class Json extends Activity{ TextView tvStatus; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(saved
public class Json extends Activity{
TextView tvStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.json);
tvStatus = (TextView) findViewById(R.id.tvStatus);
new showThat().execute("http://localhost");
}
public String readLocalhost() throws ClientProtocolException, IOException
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://localhost");
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
String data = status.toString();
return data;
}
public class showThat extends AsyncTask<String, String, String>{
protected void onPreExecute(String f) {
// TODO Auto-generated method stub
f = "saffet";
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
String data = readLocalhost();
return data;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
tvStatus.setText(result);
}
}
}
我没有被不幸的错误阻止。但我不能打印状态
编辑:我使用Xampp。您无法从android设备(仿真器)访问localhost,因为您的设备上没有localhost。您必须使用PC的真实ip地址才能连接到Web服务器。 你的ip地址是192.168.127.1,你必须打电话
new showThat().execute("http://192.168.127.1");
编辑:看看这个
干杯您无法从android设备(模拟器)访问localhost,因为您的设备上没有localhost。您必须使用PC的真实ip地址才能连接到Web服务器。 你的ip地址是192.168.127.1,你必须打电话
new showThat().execute("http://192.168.127.1");
编辑:看看这个
如果您尝试连接到
http://localhost
您正试图连接到Android设备内的服务器,因此我猜您想要的是连接到计算机中运行的服务器。然后你要做的是连接到你的计算机本地IP,因此如果你的计算机本地IP是192.168.1.1,你为连接提供的URL将是http://192.168.1.1
还要记住服务器正在侦听的端口,通常本地运行的服务器的默认配置是8080或5000。因此,如果您的服务器正在侦听端口8080,并且您的计算机IP为192.168.1.1,那么您应该提供的URL将是
http://192.168.1.1:8080
如果您尝试连接到http://localhost
您正试图连接到Android设备内的服务器,所以我想你想要的是连接到你计算机上运行的服务器。然后你要做的是连接到你的计算机本地IP,因此如果你的计算机本地IP是192.168.1.1,你为连接提供的URL将是http://192.168.1.1
还要记住服务器正在侦听的端口,通常本地运行的服务器的默认配置是8080或5000。因此,如果您的服务器正在侦听端口8080,并且您的计算机IP为192.168.1.1,那么您应该提供的URL将是
http://192.168.1.1:8080
什么?您正在尝试从Android访问localhost吗?O。o@user1372908这可能是胡说八道。我只是在学习。。无目的..您确定您的服务器正在本地主机的端口80上侦听吗?我认为您应该将服务器的网络IP而不是“本地主机”WAT?您正在尝试从Android访问localhost吗?O。o@user1372908这可能是胡说八道。我只是在学习。。无目的..您确定您的服务器正在本地主机的端口80上侦听吗?我认为您应该将服务器的网络IP改为“本地主机”