Android:连接被拒绝错误
Posted
技术标签:
【中文标题】Android:连接被拒绝错误【英文标题】:Android: Connection refused error 【发布时间】:2016-12-09 08:10:07 【问题描述】:我在连接到我的服务器时收到错误“连接被拒绝”。 在浏览器中打开 url 工作正常。我的应用程序具有互联网权限。 这是堆栈跟踪:
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 "";
是应用有问题,还是我的服务器有问题?
【问题讨论】:
【参考方案1】:修复它。问题是我在我的电脑上使用了防火墙...
【讨论】:
【参考方案2】:我想我有那个确切的问题。但是,我正在通过一个 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);
【讨论】:
HttpClient 已弃用,我无法使用它。难道没有其他方法可以做到这一点吗?以上是关于Android:连接被拒绝错误的主要内容,如果未能解决你的问题,请参考以下文章
Android ADB Connection Refused(连接被拒绝)
Android ADB Connection Refused(连接被拒绝)
将 WCF 连接到 Xamarin.Android 项目时出现连接被拒绝错误