连接到我的服务器时出现错误“Connection refused”。 在浏览器中打开URL工作正常。我的应用程序具有Internet权限。 这是堆栈跟踪:
08-03 16:17:05.549 28233-28267/appname.myname.com.appname W/System: ClassLoader referenced unknown path: /data/data/appname.myname.com.appname/lib
08-03 16:17:07.336 28233-28321/appname.myname.com.appname W/System.err: java.net.ConnectException: Connection refused
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.net.Socket.connect(Socket.java:586)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.Connection.connectSocket(Connection.java:196)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.Connection.connect(Connection.java:172)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at appname.myname.com.appname.Search$Async$override.doInBackground(Search.java:63)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at appname.myname.com.appname.Search$Async$override.access$dispatch(Search.java)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at appname.myname.com.appname.Search$Async.doInBackground(Search.java:0)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at appname.myname.com.appname.Search$Async.doInBackground(Search.java:50)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
08-03 16:17:07.338 28233-28321/appname.myname.com.appname W/System.err: at java.lang.Thread.run(Thread.java:761)
这是我的班级:
public static class Async extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
url = new URL("http://localhost:80/mysql/php.php");
}catch (MalformedURLException ex){
ex.printStackTrace();
}
try {
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
Log.d("dd", "e" + line);
}catch(IOException ex){
ex.printStackTrace();
}
return "";
}
}
应用程序是问题,还是我的服务器?
答案 0 :(得分:0)
我想我确实遇到了这个问题。但是,我正在浏览一个HttpClient,它似乎与您的代码不同。我这样做是为了解决我的问题,它可能会给你一个提示:做什么:
public static void maybeCreateHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params,
REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
HttpProtocolParams.setUserAgent(params, USER_AGENT);
HttpProtocolParams.setContentCharset(params, "UTF-8");
// And the line that I addded to help me out:
HttpProtocolParams.setUseExpectContinue(params, false);
}
}
答案 1 :(得分:0)
修正了它。问题是我在电脑上使用了防火墙......