检索和排名示例(Watson服务)和Android

时间:2017-01-25 21:57:24

标签: java android ibm-watson retrieve-and-rank

我试图用IBM Watson的Retrieve和Rank服务创建一个简单的项目。我已经收集了Collection,Cluster,并且Ranker已经过培训。后来我想向我的服务提出一个问题,但是我对org.apache.httcomponents的依赖关系有问题:httpclient:4.4.1,没有一些类。我尝试过这些例子:

  1. https://www.ibm.com/watson/developercloud/retrieve-and-rank/api/v1/?java#query_standard
  2. https://github.com/watson-developer-cloud/java-sdk/tree/master/retrieve-and-rank
  3. github.com/watson-developer-cloud/java-sdk/blob/master/examples/retrieve-and-rank-solrj/src/main/java/com/ibm/watson/developer_cloud/retrieve_and_rank/RetrieveAndRankSolrJExample.java
  4. 但我无法获得任何内容,只有群集列表。

    在gradle配置中,我有两个警告:

      

    警告:依赖org.apache.httpcomponents:httpclient:4.4.1因调试而被忽略,因为它可能与Android提供的内部版本冲突。

         

    警告:依赖org.apache.httpcomponents:httpclient:4.4.1因调试而被忽略,因为它可能与Android提供的内部版本冲突。

    你知道吗?这是我的代码:

    摇篮:应用

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.example.testrnr"
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
    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'
    }
    useLibrary 'org.apache.http.legacy'
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile 'com.ibm.watson.developer_cloud:java-sdk:3.5.3'
        compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
        //compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
        compile 'org.apache.solr:solr-solrj:5.5.1'
    
        testCompile 'junit:junit:4.12'
    }
    

    这是我的MainActivity类:

    package com.example.testrnr;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank;
    
    import org.apache.http.HttpException;
    import org.apache.http.HttpHost;
    import org.apache.http.HttpRequest;
    import org.apache.http.HttpRequestInterceptor;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.AuthState;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.client.protocol.HttpClientContext;
    import org.apache.http.impl.auth.BasicScheme;
    
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.protocol.HttpCoreContext;
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.impl.HttpSolrClient;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocumentList;
    
    import java.net.URI;
    
    public class MainActivity extends AppCompatActivity {
    
    private static final String USERNAME_CLUSTER = "**********";
    private static final String PASSWORD_CLUSTER = "**********";
    private static final String SOLR_CLUSTER_ID = "**********";
    private static final String RANKER_ID = "**********";
    private static final String COLLECTION_N= "**********";
    
    private static HttpSolrClient solrClient;
    private static RetrieveAndRank service;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            new Thread(new Runnable() {
                @Override
                public void run() {
    
                    try {
                        service = new RetrieveAndRank();
                        service.setUsernameAndPassword(USERNAME_CLUSTER, PASSWORD_CLUSTER);
    
                        Log.d("APP_TestRnR", "Clusters: " + service.getSolrClusters().execute());
    
                        solrClient = getSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), USERNAME_CLUSTER, PASSWORD_CLUSTER);
                        //SolrQuery query = new SolrQuery("*:*");
                        SolrQuery query = new SolrQuery();
                        query.setQuery("Que es la gripe?");
                        query.setParam("ranker_id", RANKER_ID);
    
                        QueryResponse response = solrClient.query(COLLECTION_N, query);
                        Log.d("APP_TestRnR", "--RESPUESTA--" + response);
    
                    } catch (Exception e) { e.printStackTrace(); }
                }}).start();
    
        }
    
        private static HttpSolrClient getSolrClient(String uri, String username, String password) {
            return new HttpSolrClient(service.getSolrUrl(SOLR_CLUSTER_ID), createHttpClient(uri, username, password));
        }
    
        private static HttpClient createHttpClient(String uri, String username, String password) {
            final URI scopeUri = URI.create(uri);
    
            final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()),
                    new UsernamePasswordCredentials("{username}", "{password}"));
    
            final HttpClientBuilder builder = HttpClientBuilder.create()
                    .setMaxConnTotal(128)
                    .setMaxConnPerRoute(32)
                    .setDefaultRequestConfig(RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build())
                    .setDefaultCredentialsProvider(credentialsProvider)
                    .addInterceptorFirst(new PreemptiveAuthInterceptor());
            return builder.build();
        }
    
        private static class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
            public void process(final HttpRequest request, final HttpContext context) throws HttpException {
                final AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    
                if (authState.getAuthScheme() == null) {
                    final CredentialsProvider credsProvider = (CredentialsProvider) context
                            .getAttribute(HttpClientContext.CREDS_PROVIDER);
                    final HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
                    final Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(),
                            targetHost.getPort()));
                    if (creds == null) {
                        throw new HttpException("No creds provided for preemptive auth.");
                    }
    
                    authState.update(new BasicScheme(), creds); //**problems here** at update method
    
                }
            }
        }
    }
    

    Android Studio表示不推荐使用此类:HttpClient,BasicCredentialsProvider,UsernamePasswordCredentials,AuthState,CredentialsProvider,Credentials,BasicScheme

0 个答案:

没有答案
相关问题