实际上,我在这里发布了太多关于连接网站的问题。我很抱歉,伙计们。
这是我的 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。