此问题是Max HTTP Connections的扩展
Web工作者给这个等式带来了什么样的影响,限制会增加吗?如同3个网络工作者能够发送(3*浏览器限制)请求?
可能重复:
使用HttpRequest.execute()的异常:无效使用SingleClientConnManager:仍然分配连接
我在Android工作.我创建了HttpSingleton类来在我的完整应用程序中创建HttpClient的单个intance.
这是我使用这个类的代码: -
HttpGet get = new HttpGet("url/dologin/savitagupta/savitagupta");
**HttpResponse rp = HttpSigleton.getInstance().execute(get);**
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// some code here
}
Run Code Online (Sandbox Code Playgroud)
这是我的单例实例
public class HttpSigleton {
private static HttpClient instance = null;
protected HttpSigleton() {
}
public static HttpClient getInstance() {
if(instance == null) {
instance =new DefaultHttpClient();
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
然后发生错误是: -
SingleClientConnManager:无效使用SingleClientConnManager:仍然分配了连接.确保在分配另一个连接之前释放连接.请告诉我我做了什么错误.我真的需要你的帮助.先感谢您.
我知道关于这个话题有几个问题但我没有找到任何答案.
我正在尝试打开与本地服务器的连接,但我一直拒绝连接.
我有服务器运行,我测试了与浏览器的连接和一个名为Postman的谷歌应用程序,它的工作原理.
打开连接时失败,好像没有任何东西要连接.或者有什么东西阻止连接?我测试了防火墙和防病毒软件,没有运气.
在Postman中测试URL返回一个用户应该...
如果我用" http://www.google.com " 替换网址它工作正常.
这是我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author Gabriel
*/
public class HttpConnection {
public HttpConnection() {
}
public void makeRequest() throws MalformedURLException, IOException {
String url = "http://localhost:8000/users/1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); …Run Code Online (Sandbox Code Playgroud) 我得到以下完整的错误消息:输入调度超时(等待发送非键事件,因为触摸的窗口尚未完成处理超过500.0ms前传递给它的某些输入事件.等待队列长度:2.等待队列头年龄:9379.7ms.)
请参阅下面的代码+跟踪.似乎锁定发生在GetIP_WAN.java:32,这是一行:
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
Run Code Online (Sandbox Code Playgroud)
每当我检测到网络更改wifi-3g/4g-没有互联网时,我都会调用此功能.在其他一些地方.它发生在每一个场合,但更多来自网络变化检测显然.
我验证公共IP是否已经改变了这种方式:
ipwan = giw.getWanIpAddress();
Run Code Online (Sandbox Code Playgroud)
在getWanIpAddress中:
ipwan = new GetIP_WAN().execute().get();
Run Code Online (Sandbox Code Playgroud)
ipwan是指向公共IP的String.
我无法重现这个ANR.
我的应用程序使用几个异步任务,可能是几秒钟的密集异步任务.如果我用强负载测试它,我关掉wifi我没有问题.
输入将非常感谢!!!
public class GetIP_WAN extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params) {
URL url;
String ipwan = null;
try {
// get URL content
url = new URL("http://ipv4bot.whatismyipaddress.com/");
URLConnection conn = url.openConnection();
conn.setConnectTimeout(3000);
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
ipwan = br.readLine();
br.close(); …Run Code Online (Sandbox Code Playgroud) 套接字只是两台机器之间的连接吗?如果可以建立套接字,为什么我们甚至使用HTTPConnection?
我有一个连接到URL的线程来获取一些数据.
有时候这个方法花了httpConnection.connect();太多的时间来获得响应,我想将这个连接线程的加载对话框限制为5 seg.
我尝试在代码中添加超时,但它不起作用 !!
URL formattedUrl = new URL(url);
URLConnection connection = formattedUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setAllowUserInteraction(false);
httpConnection.setInstanceFollowRedirects(true);
httpConnection.setRequestMethod("GET");
httpConnection.setConnectTimeout(5000);
httpConnection.setReadTimeout(5000);
httpConnection.connect();
Run Code Online (Sandbox Code Playgroud)
因此,我必须在5000秒过后或者当用户按下手机上的后退键时停止连接方法和线程.
怎么能实现这一目标?我找不到有关在Android中使用url连接线程执行此工作的信息.
谢谢
我正在尝试配置外部Web服务调用时的超时.我在我的服务中通过Spring Rest Template调用外部Web服务.
对于连接超时测试目的,外部Web服务已停止,应用程序服务器已关闭.
我已经为超时配置了10秒,但不幸的是我在一秒钟之后得到连接拒绝异常.
try {
final RestTemplate restTemplate = new RestTemplate();
((org.springframework.http.client.SimpleClientHttpRequestFactory)
restTemplate.getRequestFactory()).setReadTimeout(1000*10);
((org.springframework.http.client.SimpleClientHttpRequestFactory)
restTemplate.getRequestFactory()).setConnectTimeout(1000*10);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(reqJSON, headers);
ResponseEntity<String> response = restTemplate.exchange(wsURI, HttpMethod.POST, entity, String.class);
String premiumRespJSONStr = response.getBody();
}
Run Code Online (Sandbox Code Playgroud)
请更正我的理解.
HttpURLConnection con = null;
Response response = new Response();
String TAG = "HttpConHandler";
try{
/*
* IMPORTANT:
* User SHOULD provide URL Encoded Parms
*/
Log.p(TAG, "URL="+ urlStr);
String q=httpHeaders.get("Authorization");
URL url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", q);
con.setRequestProperty("GData-Version", "3.0");
Run Code Online (Sandbox Code Playgroud)
嗨,我在调用方法时遇到java.lang.IllegalStateException: Cannot set method after connection is made错误setRequestProperty,但是当我在连接之前调用此方法时,我得到了NullPointerException因为con为空。我能做些什么来解决这个问题?
我的一个应用程序使用大量HTTP请求与其后端进行通信.我使用2种不同的实现来执行这些请求:
Volley大多数情况下的库AsyncTask,并DefaultHttpClient为少数病例大多数时候,一切都运作良好.但有时我会提出一系列网络异常,并将其展示给Crashlytics:
java.net.UnknownHostException: Unable to resolve host "mydomain.com": No address associated with hostname
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:175)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)
通过一些研究,我发现这应该发生在设备有一个非常糟糕的3g/4g或在VPN /子网后面,所以它无法到达我的网站.
如何确保设备真正连接到互联网?我实际上只有在此函数返回true时才执行这些请求:
public static boolean isOnline(Context ctx) {
ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
或者我应该放手,并假设每月达到几百个这样的警告是正常的?
java android httpconnection android-networking android-volley
如何在UI线程和后台线程之间进行线程间通信?我想在这里使用通用处理程序概念来更新我的UI.我有如下概念
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
Run Code Online (Sandbox Code Playgroud)
我想使用两个类,一个类包含主线程,另一个类包含后台线程,它们使用相同的处理程序.我该如何实现?我知道这很常见,但我发现很难完全实现.
httpconnection ×10
java ×7
android ×6
html5 ×1
httpclient ×1
resttemplate ×1
runnable ×1
sockets ×1
spring ×1
web-worker ×1