簡體   English   中英

Android中的AWS cloudsearch

[英]AWS cloudsearch in android

我打算在Android應用程序中實現預測搜索功能,我使用AWS作為后端,我了解AWS雲搜索,請有人讓我知道如何從Android訪問cloudsearch,我使用AWS Android SDK for發展。

我知道如何配置Cloudsearch並添加建議器。 只有我需要如何獲得建議,例如,如果用戶鍵入“foo”它應該返回建議

您可以設置並執行如下所示的請求:

final String SERVICE_NAME = "cloudsearch";

// Replace AnonymousAWSCredentials with whatever kind of credentials you really want
AnonymousAWSCredentials anonCredentials = new AnonymousAWSCredentials();
final String searchHost = "https://<CloudSearch endpoint goes here>.eu-west-1.cloudsearch.amazonaws.com";

ClientConfiguration clientConfiguration = new ClientConfiguration();

Request request = new DefaultRequest(SERVICE_NAME);
request.setEndpoint(URI.create(searchHost));
request.setResourcePath("/2013-01-01/search");
request.setHttpMethod(HttpMethodName.GET);
request.addParameter("q", searchTerms); // searchTerms is whatever you want to search for

request.addHeader("Content-Type", "application/json");

AWS4Signer signer = new AWS4Signer();
signer.setServiceName(SERVICE_NAME);
signer.setRegionName(Region.getRegion(Regions.EU_WEST_1).getName());
// Make sure your Request here matches that in searchHost above

ExecutionContext executionContext = new ExecutionContext();

signer.sign(request, anonCredentials);

AmazonHttpClient webClient = new AmazonHttpClient(clientConfiguration);
webClient.execute(request, new HttpResponseHandler<AmazonWebServiceResponse<String>>() {
                @Override
                public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception {
                    Log.v(TAG, "Successful response!");
                    return null;
                }

                @Override
                public boolean needsConnectionLeftOpen() {
                    return false;
                }
            }, new HttpResponseHandler<AmazonServiceException>() {
                @Override
                public AmazonServiceException handle(HttpResponse response) throws Exception {
                    BufferedReader r = new BufferedReader(new InputStreamReader(response.getContent()));
                    StringBuilder total = new StringBuilder();
                    for (String line; (line = r.readLine()) != null; ) {
                        total.append(line).append('\n');
                    }

                    Log.v(TAG, "Error response: " + response.getStatusText() + " / " + total.toString());
                    return null;
                }

                @Override
                public boolean needsConnectionLeftOpen() {
                    return false;
                }
            }, executionContext);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM