在Android 上使用SolrJ

  • 本文内容: 在Android上使用SolrJ库来连接Solr服务器检索数据,主要问题就在Solrj使用的HTTPClient在Android上的匹配问题,Android项目的配置,其中遇到的坑比较多。

SolrJ是什么

SolrJ 是Solr 提供的Java API

相关资源

Android 模块 build.gradle 配置

主要有如下注意点:

  1. 需要添加packagingOptions 段
  2. 需要添加 useLibrary 'org.apache.http.legacy' 以启用Apache HttpClient
  3. 添加compile 'org.apache.httpcomponents:httpclient-android:4.3.5' 使用此库作为HTTPClient的实现,同时要排除 solr-solrjhttpmime 库中会冲突的部分
android {
    compileSdkVersion 24
    buildToolsVersion '24.0.1'
    //...... 省略其他配置
    packagingOptions {
        // 可能有多余的
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
   //使用Apache HTTP 客户端 ,因为android6.0 把这个移除了
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    // 省略其他配置
    // 查询依赖 :https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime/4.5.3
    compile('org.apache.httpcomponents:httpmime:4.5.3') {
        exclude module: 'httpclient'
    }
    //查询依赖 :https://mvnrepository.com/artifact/org.apache.solr/solr-solrj/5.3.0
    compile('org.apache.solr:solr-solrj:5.3.+') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
        // httpcore 和 httpclient-android 有冲突
        exclude group: 'org.apache.httpcomponents', module: 'httpcore'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
}

Java SolrJ测试代码

HttpClient httpClient = new DefaultHttpClient();
// 设置超时时间 ,SolrClient 的用不了
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
SolrClient solrClient = new HttpSolrClient(mServiceUrl,httpClient );
SolrQuery query = makeSolrQuery(queryParams,-1,-1);
QueryResponse solrResponse = null;
try {
            solrResponse = solrClient.query(query);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
}
if(solrResponse == null){
            throw new Exception("查询失败");
}


private SolrQuery makeSolrQuery(String queryParams, int start, int rows) {
        SolrQuery query = new SolrQuery(queryParams);
        if(start >= 0){
            query.set("start",start);
        }
        if(rows > 0) {
            query.set("rows", rows);
        }
        query.set("timeAllowed",30*1000);//设置timeout 毫秒
        return query;
}

相关问题日志关键词:

  • ERR: stack=java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/invoke/MethodHandles;
  • NoSuchFieldError INSTANCE org/apache/http/message/BasicHeaderValueParser
  • 使用SolrClient的设置timeout方法报错: java.lang.VerifyError: Verifier rejected class org.apache.solr.client.solrj.impl.HttpClientUtil due to bad method org.apache.http.impl.client.CloseableHttpClient org.apache.solr.client.solrj.impl.HttpClientUtil.createClient(org.apache.solr.common.params.SolrParams, org.apache.http.conn.ClientConnectionManager) (declaration of 'org.apache.solr.client.solrj.impl.HttpClientUtil'

参考链接:

StackOverFlower 问题1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,169评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,134评论 25 709
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光剑书架上的书阅读 3,985评论 2 8
  • 最近迷恋上一个女人写的书,她叫王潇,人称“潇洒姐”,比我大几岁,但作为拥有上百万粉丝的励志姐,还建立了自己的...
    点播时光阅读 220评论 0 0