Android - HttpClient.execute(HttpPost)随机抛出IOException(连接被拒绝)

时间:2015-06-24 07:07:57

标签: android android-asynctask androidhttpclient

我已经创建了一个类来处理从print_pol扩展的API请求。随机部分AsyncTask<String,String,byte[]>抛出IO错误消息response = client.execute(post);

Connection to [URL] refused

这些是一些记录的文本

@Override protected byte[] doInBackground(String... uri) {
    ArrayList<NameValuePair> list = this.params==null?null:new ArrayList<NameValuePair>(this.params.size());
    byte[] result = null;
    HashMap<String,Object> iterated = this.params==null?null:(HashMap<String, Object>) this.params.clone();
    if(iterated!=null&&iterated.size()!=0) try{
        Iterator iterator = iterated.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry pair = (Map.Entry)iterator.next();
            String key = String.valueOf(pair.getKey());
            String val = String.valueOf(pair.getValue());
            if(list!=null) list.add(new BasicNameValuePair(key,val));
            iterator.remove();
        }
    }catch(UnsupportedEncodingException ex){
        return null;
    }
    HttpPost post = new HttpPost(this.url);
    try{
        if(list!=null) post.setEntity(new UrlEncodedFormEntity(list));
    }catch(UnsupportedEncodingException ex){
        return null;
    }
    if(isCancelled()||post.isAborted()) return null;
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, this.timeout);
    HttpClient client = new DefaultHttpClient(httpParams);
    HttpResponse response;
    try{
        response = client.execute(post);
    }catch(ClientProtocolException ex){
        return null;
    }catch(IOException ex) {
        return null;
    }
    StatusLine status = response.getStatusLine();
    if(isCancelled()||post.isAborted()) return null;
    if(status.getStatusCode() == HttpStatus.SC_OK) try{
        if(response.getEntity()==null) return null;
        Header responsetype = response.getEntity().getContentType();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        result = out.toByteArray();
    }catch(IOException ex){}
    else response.getEntity().getContent().close();
    return result;
}

0 个答案:

没有答案