在Android 上使用SolrJ

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

SolrJ是什么

SolrJ 是Solr 提供的Java API

相关资源

  • Solr主页
  • Solr官方文档下载 其中有SolrJ API说明
  • SolrJ Wiki

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

你可能感兴趣的:(在Android 上使用SolrJ)