我实际上在这里发布了关于连接网站的太多问题。我很抱歉,伙计们。
这是我的Json.java
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);
}
}
}
我只是想打印状态。这里还有logcat!
10-23 14:13:53.276: D/gralloc_goldfish(1429): Emulator without GPU emulation detected.
10-23 14:13:54.057: W/System.err(1429): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
10-23 14:13:54.076: W/System.err(1429): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
10-23 14:13:54.076: W/System.err(1429): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-23 14:13:54.086: W/System.err(1429): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-23 14:13:54.086: W/System.err(1429): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-23 14:13:54.086: W/System.err(1429): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-23 14:13:54.086: W/System.err(1429): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-23 14:13:54.096: W/System.err(1429): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-23 14:13:54.096: W/System.err(1429): at com.example.channel.Json.readLocalhost(Json.java:36)
10-23 14:13:54.096: W/System.err(1429): at com.example.channel.Json$showThat.doInBackground(Json.java:55)
10-23 14:13:54.096: W/System.err(1429): at com.example.channel.Json$showThat.doInBackground(Json.java:1)
10-23 14:13:54.096: W/System.err(1429): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-23 14:13:54.106: W/System.err(1429): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-23 14:13:54.106: W/System.err(1429): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-23 14:13:54.106: W/System.err(1429): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-23 14:13:54.106: W/System.err(1429): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-23 14:13:54.116: W/System.err(1429): at java.lang.Thread.run(Thread.java:841)
10-23 14:13:54.116: W/System.err(1429): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.146: W/System.err(1429): at libcore.io.IoBridge.connect(IoBridge.java:114)
10-23 14:13:54.146: W/System.err(1429): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-23 14:13:54.146: W/System.err(1429): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-23 14:13:54.146: W/System.err(1429): at java.net.Socket.connect(Socket.java:842)
10-23 14:13:54.156: W/System.err(1429): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-23 14:13:54.156: W/System.err(1429): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-23 14:13:54.156: W/System.err(1429): ... 15 more
10-23 14:13:54.166: W/System.err(1429): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
10-23 14:13:54.206: D/dalvikvm(1429): GC_FOR_ALLOC freed 134K, 8% free 2886K/3136K, paused 43ms, total 44ms
10-23 14:13:54.216: W/System.err(1429): at libcore.io.Posix.connect(Native Method)
10-23 14:13:54.216: W/System.err(1429): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-23 14:13:54.216: W/System.err(1429): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-23 14:13:54.216: W/System.err(1429): at libcore.io.IoBridge.connect(IoBridge.java:112)
10-23 14:13:54.216: W/System.err(1429): ... 20 more
我不会因此而停止错误。但我不能打印状态。
编辑:我使用Xampp。
答案 0 :(得分:3)
如果您尝试连接到http://localhost
,您尝试连接到Android设备内的服务器,那么我想您想要的是连接到计算机中运行的服务器。那么你要做的就是连接你的计算机本地IP,所以如果你的计算机本地IP是192.168.1.1,你为连接提供的URL将是http://192.168.1.1
还要记住服务器正在侦听的端口,通常在本地运行的服务器的默认配置是8080或5000.因此,如果您的服务器正在侦听端口8080并且您的计算机IP是192.168.1.1,则您应提供的网址为http://192.168.1.1:8080
答案 1 :(得分:1)
您无法从Android设备(模拟器)访问localhost,因为您的设备上没有localhost。您必须使用PC的真实IP地址连接到您的网络服务器。 例如。您的IP地址是192.168.127.1,您必须致电
new showThat().execute("http://192.168.127.1");
编辑:看一下这个thread
干杯