- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试通过 HttpPost 连接并将用户名和密码发送到网站,然后从该网站接收字符串。我尝试了过去对我有用的各种方法,但现在当我发送用户名和密码标识符时,应用程序超时长达 4 分钟,然后吐出以下异常:
07-16 16:32:32.897: W/System.err(632): Unable to connect to the server
07-16 16:32:32.907: W/System.err(632): org.apache.http.conn.HttpHostConnectException: Connection to http://devdashboard.company refused
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-16 16:32:32.917: W/System.err(632): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-16 16:32:32.927: W/System.err(632): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-16 16:32:32.927: W/System.err(632): at company.android.dashboard.app.HttpHelperAndroid.sendToHttp(HttpHelperAndroid.java:66)
07-16 16:32:32.927: W/System.err(632): at company.android.dashboard.app.DashboardAppActivity.goToDashboard(DashboardAppActivity.java:62)
07-16 16:32:32.927: W/System.err(632): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:32:32.937: W/System.err(632): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:32:32.937: W/System.err(632): at android.view.View$1.onClick(View.java:3039)
07-16 16:32:32.947: W/System.err(632): at android.view.View.performClick(View.java:3511)
07-16 16:32:32.947: W/System.err(632): at android.view.View$PerformClick.run(View.java:14105)
07-16 16:32:32.947: W/System.err(632): at android.os.Handler.handleCallback(Handler.java:605)
07-16 16:32:32.957: W/System.err(632): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 16:32:32.957: W/System.err(632): at android.os.Looper.loop(Looper.java:137)
07-16 16:32:32.967: W/System.err(632): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 16:32:32.977: W/System.err(632): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 16:32:32.977: W/System.err(632): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 16:32:32.977: W/System.err(632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 16:32:32.987: W/System.err(632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 16:32:32.987: W/System.err(632): at dalvik.system.NativeStart.main(Native Method)
07-16 16:32:32.987: W/System.err(632): Caused by: java.net.ConnectException: failed to connect to /50.19.240.232 (port 80): connect failed: ETIMEDOUT (Connection timed out)
07-16 16:32:32.997: W/System.err(632): at libcore.io.IoBridge.connect(IoBridge.java:114)
07-16 16:32:32.997: W/System.err(632): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-16 16:32:32.997: W/System.err(632): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-16 16:32:33.007: W/System.err(632): at java.net.Socket.connect(Socket.java:842)
07-16 16:32:33.007: W/System.err(632): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-16 16:32:33.017: W/System.err(632): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-16 16:32:33.017: W/System.err(632): ... 22 more
07-16 16:32:33.027: W/System.err(632): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
07-16 16:32:33.047: W/System.err(632): at libcore.io.Posix.connect(Native Method)
07-16 16:32:33.047: W/System.err(632): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
07-16 16:32:33.047: W/System.err(632): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-16 16:32:33.057: W/System.err(632): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-1
6 16:32:33.057: W/System.err(632): ... 27 more
我的 XML list 文件中启用了 Internet 权限
我目前的实现是这样的:
String LOGIN = "email@gmail.com";
String PASSWORD ="password1";
//JSONObject to send the username and pw
JSONObject json = new JSONObject();
//put the path in the JSONArray object
JSONArray vect = new JSONArray();
vect.put("company Android Library");
vect.put("Rocket Ship");
int duration = 50;
try {
json.put("email", LOGIN);
json.put("password", PASSWORD);
json.put("json", "true");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, "ABOUT TO SEND:" + json.toString());
JSONObject inJson = HttpHelperAndroid.sendToHttp(json, "http://devdashboard.company/login");
if(inJson != null)
{
Log.d(TAG, "RECIEVED the JSON:" + inJson.toString());
}
else
Log.d(TAG, "THE RESPONSE WAS NULL");
}
HttpHelperAndroid 类如下所示:
public class HttpHelperAndroid
{
private static final String TAG = "HttpHelperAndroid";//TAG for the LogCat(debugging)
private static boolean responseSuccessful = true;
/**
* sends the JSONObject parameter to the desired URL parameter and gets the response
*
* @param url the URL to which the JSONObject should be sent
* @param jsonObjOut the JSONObject that is to be sent
* @return the response from the server as a JSONObject
*/
public static JSONObject sendToHttp(JSONObject jsonObjOut, String url) {
responseSuccessful = true;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(url);
//convert the JSONObject to a string
StringEntity se;
//set our StringEntity to the JSONObject as a string
se = new StringEntity(jsonObjOut.toString());
// Set HTTP params
httpRequest.setEntity(se);
httpRequest.setHeader("Accept", "application/json");
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setHeader("Accept-Encoding", "gzip"); //for gzip compression
//get the current time
long oldTime = System.currentTimeMillis();
HttpResponse response = null;
try
{
//execute the http request and get the response
response = (HttpResponse) httpClient.execute(httpRequest);
}
catch(HttpHostConnectException e)
{
System.err.println("Unable to connect to the server");
e.printStackTrace();
responseSuccessful = false;
}
//only continue executing if we got a response from the server
if(responseSuccessful)
{
//print how long the response took to the LogCat if it's on
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-oldTime) + "ms]");
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream in = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
in = new GZIPInputStream(in);
}
// convert content stream to a String
String resultString= streamToString(in);
//close the stream
in.close();
// convert the String into a JSONObject
JSONObject jsonObjRecv = new JSONObject(resultString);
//take a peak at the JSONObject we got back if the LogCat is on
Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
//return the JSONObject we got back from the server
return jsonObjRecv;
}
}
}
//catch any exception that was thrown
catch (Exception e)
{
//Print the exception
e.printStackTrace();
}
return null;
}
private static String streamToString(InputStream is)
{
//create a new BufferedReader for the input stream
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//create a new StringBuilder to append the lines
StringBuilder sb = new StringBuilder();
//initialize an empty string
String line = null;
try
{
//iterate as long as there is still lines to be read
while ((line = reader.readLine()) != null)
{
//append the line and a newline character to our StringBuilder
sb.append(line + "\n");
}
}
//catch an IOException and print it
catch (IOException e) {
e.printStackTrace();
}
//close the stream when we're done
finally
{
try
{
is.close();
}
//catch and print an exception if it's thrown
catch (IOException e)
{
e.printStackTrace();
}
}
//return the stream converted to a string
return sb.toString();
}
}
这是我的 XML 只是为了好玩:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="company.android.dashboard.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/company_android_ico"
android:label="@string/app_name" >
<activity
android:name=".DashboardAppActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我在过去的项目中使用过 HttpHelper 类并且它对我有用,此外我尝试使用 nameValuePairs 实现它:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "email@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("password", "password1"));
nameValuePairs.add(new BasicNameValuePair("json", "true"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
这产生了相同的结果。
这会不会是证书的事情?或者可能与损坏的 XML 文件有关(我尝试重新制作项目和 xml 文件)Android HTTP Connection refused或者可能是某种 Android 主机文件问题?
我愿意接受任何建议!
我已经从很多角度对此进行了研究,我很乐意提供任何其他有用的信息!非常感谢您的宝贵时间!
注意:出于安全原因,该 url 是一个虚拟 url,而不是我正在连接的实际 url。我能够使用参数从命令行 curl 实际网站并且它有效,我也能够从网络浏览器正常登录。
编辑 我已经确定了问题所在!但不幸的是,这不是解决方案。所以问题是我使用的开发服务器 url 在全局 DNS 服务器上没有域条目。因此,要解决此问题,我需要以某种方式在我的 Android 设备/模拟器中编辑主机文件...有人知道如何合法地完成此操作吗?
最佳答案
我已经找到问题了!所以问题是我使用的开发服务器 url 在全局 DNS 服务器上没有域条目。
所以这个问题有两种可能的解决方案:
1) 在 Android 设备上编辑主机文件(需要 root 手机):How to change the hosts file on android
2) 使服务器在全局 DNS 服务器上注册。(如果您不对网址负责,也很难做到)
无论如何,我希望这对其他人也有帮助!
关于android - HttpHostConnectException : Connection refused Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514038/
这个问题已经有答案了: AWS RDS How to set up a MySQL Database (1 个回答) 已关闭 6 年前。 我有一个AWS Elastic Beanstalk实例 Tom
我正在 Mean.js 版本 4.2 上运行 npm test,它在 Protractor e2e 测试中给出了“连接被拒绝”错误。我尝试更新 Selenium 像 this says to.现在它的
我购买了一台服务器(新加坡地区,512 MB RAM)我试图在该服务器中设置单节点集群。我从这个 link当我检查 $ sudo service Cassandra status它正在显示 nodet
我正在使用 spring cloud 来配置微服务。我使用 Jhipster 来生成应用程序。 我有三个应用程序 JHipster-Registry、Gateway 和 admin 应用程序 所有三个
我有 2 个 java 文件 Server.java 和 Client.java。两者都在不同的容器中。 DOCKER 文件:我用于服务器的 dockerfile(在名为“Server”的文件夹中)是
客户端应用程序将 json 数据发送到 localhost:8080 上的服务器,该数据打包并作为 Docker 镜像运行。使用 Postman chrome 应用程序手动发送 json 时,服务器工
我正在尝试关注 celery tutorial ,但是当我运行 python manage.py celeryd 时遇到了一个问题:我的 RabbitMQ 服务器(安装在我开发箱的虚拟机上)不允许我的
我清理了~/.ivy2/cache目录。 我的project/plugins.sbt文件: $ cat project/plugins.sbt // Comment to get more infor
ELK 与销售人员 URL:http://localhost:9200/>, :error_type=>LogStash::Outputs::ElasticSearch::HttpClient::Po
我正在另一个容器中使用 Dockerize spring boot 应用程序和 redis。 我使用 docker compose 在同一网络中运行两个容器,这是我的 docker-compose.y
我试图从我的应用程序访问 Rest API。当该 API 不在线时,我的应用程序发生了上述错误。我正在使用 Spring Boot 。我想知道,有什么方法可以在访问该网址之前检查该网址的可用性。 S
在我的 flutter 应用程序中,我使用 flask 服务器进行测试。我启动了我的服务器并在我的 flutter 应用程序中运行 API url。但是 SocketException: Connec
我正在 Mac 上的 IntelliJ 中设置远程调试器。我没有做任何修改就遵循了模板。然后我单击“调试 xxx”按钮。表明 "Error running 'Remote Debugger': Una
我有一个 linux box,jenkins 服务器在上面运行并从 git 中拉取脚本并开始执行 我有一台 Windows 电脑,我从它打开 jenkins url 说 xyz:8080 并尝试从我的
我在 tomcat 7.50 上有一个应用程序,在单个请求上工作正常,但在许多同时请求 (~1200) 上我得到: 2014-11-02 11:22:48,485 ERROR [MONITOR-AG
当我尝试重新配置我的 gitlab 实例时,出现了这个错误。sudo gitlab-ctl reconfigure 工作正常但是当我尝试启动 gitlab 时我看到 502 错误并且当我跟踪日志时我看
我在 3 个 n1-standard-4 GKE 实例上运行了大约 200 个 pod。流量水平较低,因此每台机器上都有大量备用 CPU 和 RAM。通常当服务尝试相互连接时,连接失败并显示“CONN
我在unbundu机器中使用JMeter设置了分布式负载测试环境。 ->主站:系统运行JMeter GUI,控制每个从站。 ->从站:运行jmeter-server的系统,从主站接收命令,并向被测服务
我需要帮助来解决被拒绝的状态。我查看了 named.conf,一切正常。 我什至把allow-query改成了any,原来是localhost。 dig xxx.com @ns1.xxx.com ;
我正在使用Laravel。当我dd($request->all())时,其中的数据涉及文件和其他一些数据。它返回错误 [2019-02-22 19:40:24] local.ERROR: stream
我是一名优秀的程序员,十分优秀!