Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sotimeout在android 2.1上的多部分http post中不起作用_Android - Fatal编程技术网

sotimeout在android 2.1上的多部分http post中不起作用

sotimeout在android 2.1上的多部分http post中不起作用,android,Android,我正在使用android sdk附带的apache httpclient,使用多部分http post在服务器上上传文件。问题是,当我关闭设备上的wifi连接,并且在设置sotimeout和connectiontimeout后设备没有internet访问和事件时,代码无限期挂起在httpclient.execute()语句上,并且每次都会发生 我的代码是: HttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams

我正在使用android sdk附带的apache httpclient,使用多部分http post在服务器上上传文件。问题是,当我关闭设备上的wifi连接,并且在设置sotimeout和connectiontimeout后设备没有internet访问和事件时,代码无限期挂起在
httpclient.execute()
语句上,并且每次都会发生

我的代码是:

HttpClient httpclient = new DefaultHttpClient();

HttpConnectionParams.setSoTimeout(httpclient.getParams(), 5000);
ConnManagerParams.setTimeout( httpclient.getParams(), 5000 ); 

HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 8192);

HttpPost("http://myurl");

File file = new File(fileAbsolutePath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uploadedfile", cbFile);
httppost.setEntity(mpEntity);
if(!backupCancel)
{
    System.out.println("<<<<<<<<<<<<<<<<<<<<<<Actually transferring file>>>>>>>>>>>>>>>");
    HttpResponse response = httpclient.execute(httppost);
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpConnectionParams.setSoTimeout(httpclient.getParams(),5000);
setTimeout(httpclient.getParams(),5000);
HttpConnectionParams.setSocketBufferSize(httpclient.getParams(),8192);
HttpPost(“http://myurl");
File File=新文件(fileAbsolutePath);
MultipartEntity mpEntity=新的MultipartEntity();
ContentBody cbFile=新文件体(文件);
addPart(“uploadedfile”,cbFile);
httppost.setEntity(mpEntity);
如果(!备份取消)
{

System.out.println(“我认为您应该使用

HttpParams lHttpParams = new  BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(lHttpParams, 3000);
HttpConnectionParams.setSoTimeout(lHttpParams, 3000);
致以最诚挚的问候,
~Anup

试试这个:

假设您有一个httpClient对象,它是AndroidHttpClient或DefaultHttpClient的实例

HttpParams httpParams = httpClient.getParams();
httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Timeout in milli seconds);
您得到的httpParams是一个引用对象,因此它执行setIntParameter将修复该问题


除非您对自定义超时有特殊要求,否则您更喜欢使用AndroidHttpClient,它非常有用,并解决了我们的大多数问题:)祝您好运

您是否尝试过此方法……我尝试过所有可能的组合,但似乎都没有正确设置超时。当设备没有互联网连接时,它永远不会生效:(是的,你给的东西很好…实际上我正在检查的设备是一个破裂的设备,上面强制安装了2.2…可能是因为我在其他设备上检查时,它工作得很好..thanx