Android中的AWS cloudsearch

问题描述 投票:3回答:1

我计划在Android应用程序中实现预测搜索功能,我使用AWS作为后端,我了解AWS云搜索,请有人让我知道如何从Android访问cloudsearch,我使用AWS Android SDK for发展。

我知道如何配置Cloudsearch并添加建议器。只有我需要如何获得建议,例如,如果用户键入“foo”它应该返回建议

android amazon-web-services amazon-cloudsearch
1个回答
0
投票

您可以设置并执行如下所示的请求:

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);
© www.soinside.com 2019 - 2024. All rights reserved.