Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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
Android 异步任务处理长http调用_Android_Android Asynctask - Fatal编程技术网

Android 异步任务处理长http调用

Android 异步任务处理长http调用,android,android-asynctask,Android,Android Asynctask,公共类NetworkService扩展了异步任务{ } 好的,这是api调用,我正在使用 基本上,我进行http调用。但如果这个http需要很长时间(在本例中为30秒),我会停止它..并显示连接到服务器的错误。 但是,一旦出现此弹出窗口,我就会遇到问题…我拨打的下一个http呼叫也无法工作。我被迫删除我的应用程序,并重新安装以使其工作。 有什么帮助吗 这里还有service handler.java 公共类ServiceHandler{ 静态字符串响应=null; 公共最终静态int GET=1

公共类NetworkService扩展了异步任务{

}

好的,这是api调用,我正在使用

基本上,我进行http调用。但如果这个http需要很长时间(在本例中为30秒),我会停止它..并显示连接到服务器的错误。 但是,一旦出现此弹出窗口,我就会遇到问题…我拨打的下一个http呼叫也无法工作。我被迫删除我的应用程序,并重新安装以使其工作。 有什么帮助吗

这里还有service handler.java

公共类ServiceHandler{

静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
* */
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
*@params-http请求参数
* */
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
//http客户端
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
addHeader(“缓存控制”,“无缓存”);
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
addHeader(“缓存控制”,“无缓存”);
httpResponse=httpClient.execute(httpGet);
}
httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}

}

在发出第二个请求之前,第一个HTTP连接仍处于活动状态。使用下面的代码,而不是直接使用DefaultHttpClient

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 200000);
    HttpConnectionParams.setSoTimeout(params, 200000);
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
            mgr.getSchemeRegistry()), params);

    return client;
}
现在从代码块中修改这一行


DefaultHttpClient httpClient=getThreadSafeClient()

这是我在你的答案上得到的错误

09-17 16:58:14.539:I/System.out(29210):Apache Tomcat/7.0.27-错误报告HTTP状态500-类型异常报告

消息

说明服务器遇到内部错误(),无法完成此请求。

异常组织.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.springframework.web.bind.annotation.support.HandlerMethodInvocationException:调用处理程序方法失败[public java.lang.String com.netvariant.zain.sa.mobapp.api.v1.auth.AuthController.registerDevice(java.lang.String,java.lang.String,java.lang.String)抛出java.lang.exception];嵌套异常为java.lang.IllegalStateException:缺少类型为[java.lang.String]的标头“用户代理”


200000意味着200秒,对吗?虽然使用此工具,但我遇到了一个错误。url返回给我错误的东西…然后正常。是的,它是200秒。此外,此代码段取自我以前的一个项目,因此应该可以正常工作。请再试一次,如果代码仍然不起作用,请与我共享代码。
          final NetworkService networkCall = new NetworkService(url);
             networkCall.delegate = this;
            networkCall.execute();
            Handler handler = new Handler();
            detect = 0;
            handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            try
                            {
                                if (networkCall.getStatus() == NetworkService.Status.RUNNING)
                                {
                                    networkCall.cancel(true);
                                    ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
                                    progress.setVisibility(View.INVISIBLE);
                                    Toast toast = Toast.makeText(BBActivity.this, R.string.serverError, Toast.LENGTH_LONG);
                                    toast.show();
                                                             }
                            }
                            catch(Exception tre)
                            {       
                            }
                        }
                    }, 30000);
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            httpPost.addHeader("Cache-Control", "no-cache");
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("Cache-Control", "no-cache");
            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}
public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 200000);
    HttpConnectionParams.setSoTimeout(params, 200000);
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
            mgr.getSchemeRegistry()), params);

    return client;
}