本文整理了Java中com.google.api.client.http.HttpResponse.parseAsString()
方法的一些代码示例,展示了HttpResponse.parseAsString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.parseAsString()
方法的具体详情如下:
包路径:com.google.api.client.http.HttpResponse
类名称:HttpResponse
方法名:parseAsString
暂无
代码示例来源:origin: googleapis/google-cloud-java
private static String getAppEngineProjectIdFromMetadataServer() throws IOException {
String metadata = "http://metadata.google.internal";
String projectIdURL = "/computeMetadata/v1/project/project-id";
GenericUrl url = new GenericUrl(metadata + projectIdURL);
HttpTransport netHttpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory();
HttpRequest request =
requestFactory
.buildGetRequest(url)
.setConnectTimeout(500)
.setReadTimeout(500)
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
HttpResponse response = request.execute();
return headerContainsMetadataFlavor(response) ? response.parseAsString() : null;
}
代码示例来源:origin: stackoverflow.com
String STORAGE_SCOPE = "https://www.googleapis.com/auth/devstorage.read_write";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
Log.d("testing", "checking if I can create a credential");
httpTransport = AndroidHttp.newCompatibleTransport();
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(resources_.openRawResource(R.raw.gcs_privatekey),
"password".toCharArray());
PrivateKey key = (PrivateKey) keystore.getKey("privatekey", "password".toCharArray());
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountPrivateKey(key)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
// .setServiceAccountUser(SERVICE_ACCOUNT_EMAIL)
// .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.build();
credential.refreshToken();
String URI = "https://storage.googleapis.com/" + BUCKET_NAME;
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
Log.d("testing", "response content is: " + content);
new Storage.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName("appname").build();
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
/**
* Fetches the listing of the given bucket.
*
* @param bucketName the name of the bucket to list.
*
* @return the raw XML containing the listing of the bucket.
* @throws IOException if there's an error communicating with Cloud Storage.
* @throws GeneralSecurityException for errors creating https connection.
*/
public static String listBucket(final String bucketName)
throws IOException, GeneralSecurityException {
//[START snippet]
// Build an account credential.
GoogleCredential credential = GoogleCredential.getApplicationDefault()
.createScoped(Collections.singleton(STORAGE_SCOPE));
// Set up and execute a Google Cloud Storage request.
String uri = "https://storage.googleapis.com/"
+ URLEncoder.encode(bucketName, "UTF-8");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(
credential);
GenericUrl url = new GenericUrl(uri);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse response = request.execute();
String content = response.parseAsString();
//[END snippet]
return content;
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);
String response = request.execute().parseAsString();
System.out.println(response);
代码示例来源:origin: google/google-api-java-client-samples
String response = request.execute().parseAsString();
System.out.println(response);
} catch (IOException e) {
代码示例来源:origin: com.onpositive.aml/org.aml.registry
public String get() throws InterruptedException, ExecutionException {
try {
return rs.get().parseAsString();
} catch (IOException e) {
throw new IllegalStateException();
}
}
代码示例来源:origin: com.onpositive.aml/org.aml.registry
public String get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
try {
return rs.get(timeout, unit).parseAsString();
} catch (IOException e) {
throw new IllegalStateException();
}
}
};
代码示例来源:origin: stackoverflow.com
HttpTransport transport = new NetHttpTransport();
HttpRequestFactory httpRequestFactory = createRequestFactory (HTTP_TRANSPORT);
String ll = String.valueOf(df.format(lonlat))+","+String.valueOf(df.format(lonlat2));
url = (API_URL+ "venues/search?ll="+ll+"&client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET+"&v"+v);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(url));
System.out.println(request);
HttpResponse httpResponse = request.execute();
JSONObject object = new JSONObject(httpResponse.parseAsString());
JSONObject foursquareResponse = (JSONObject) object.get("response");
JSONArray groups = (JSONArray) foursquareResponse.get("groups");
JSONObject group = (JSONObject)groups.get(0);
JSONArray items = (JSONArray)group.get("items");
代码示例来源:origin: stackoverflow.com
// httpbin.org is a wonderful URL to test API calls against as it returns whatever if received.
GenericUrl url = new GenericUrl("http://httpbin.org/put");
JsonHttpContent content = new JsonHttpContent(new JacksonFactory(), myUser);
HttpRequest request = requestFactory.buildPutRequest(url, content);
HttpResponse response = request.execute();
System.out.println(response.parseAsString());
代码示例来源:origin: com.onpositive.aml/org.aml.registry
public static String readString(String url) throws IOException {
HttpRequest buildGetRequest;
buildGetRequest = createRequestFactory.buildGetRequest(new GenericUrl(url));
String str=buildGetRequest.execute().parseAsString();
return str;
}
代码示例来源:origin: stackoverflow.com
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
String query = "[{\"limit\": 5,\"name\":null,\"type\":\"/medicine/disease\"}]";
GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
url.put("key", "YOUR-API-KEY-GOES-HERE");
url.put("query", query);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString());
JSONArray results = (JSONArray)response.get("result");
for (Object result : results) {
System.out.println(result.get("name").toString());
}
代码示例来源:origin: k0kubun/gitstar-ranking
private JsonObject graphql(String query)
throws IOException
{
String payload = Json.createObjectBuilder()
.add("query", query)
.add("variables", "{}")
.build().toString();
HttpRequest request = requestFactory.buildPostRequest(
new GenericUrl(GRAPHQL_ENDPOINT),
ByteArrayContent.fromString("application/json", payload));
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("bearer " + tokenFactory.getToken());
request.setHeaders(headers);
HttpResponse response = executeWithRetry(request);
// TODO: Handle error status code
JsonObject responseObject = Json.createReader(new StringReader(response.parseAsString())).readObject();
if (responseObject.containsKey("errors")) {
LOG.debug("errors with query:\n" + query);
LOG.debug("response:\n" + responseObject.toString());
}
return responseObject;
}
代码示例来源:origin: UglyTroLL/Douban-Java-SDK-OAuth2
public String postEncodedEntry (String url, Map<String, String> params, boolean needAccessToken) throws DoubanException, IOException {
UrlEncodedContent content = new UrlEncodedContent(params);
HttpRequest method = requestFactory.buildPostRequest(new GenericUrl(url), content);
return httpRequest(method, needAccessToken).parseAsString();
}
代码示例来源:origin: edu.illinois.cs.cogcomp/wikiutils
public JSONObject getCursorAndResponse(String mqlQuery, String cursor)
throws IOException, ParseException {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport
.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl(
"https://www.googleapis.com/freebase/v1/mqlread");
url.put("query", mqlQuery);
url.put("key", apikey);
url.put("cursor", cursor);
logger.debug("QUERY URL: " + url.toString());
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject) parser.parse(httpResponse
.parseAsString());
return response;
}
代码示例来源:origin: UglyTroLL/Douban-Java-SDK-OAuth2
public String postMultipartEntry (String url, Map<String, String> params, boolean needAccessToken) throws DoubanException, IOException {
UrlEncodedContent uec = new UrlEncodedContent(params);
MultipartRelatedContent content = new MultipartRelatedContent(uec);
HttpRequest method = requestFactory.buildPostRequest(new GenericUrl(url), content);
return httpRequest(method, needAccessToken).parseAsString();
}
代码示例来源:origin: com.google.endpoints/endpoints-management-config
private static Service parseHttpResponse(HttpResponse httpResponse) {
try {
Builder builder = Service.newBuilder();
JsonFormat.parser().merge(httpResponse.parseAsString(), builder);
return builder.build();
} catch (IOException exception) {
throw new ServiceConfigException(
"Failed to parse the HTTP response as service configuration",
exception);
}
}
代码示例来源:origin: CogComp/cogcomp-nlp
public JSONObject getCursorAndResponse(String mqlQuery, String cursor)
throws IOException, ParseException {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport
.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl(
"https://www.googleapis.com/freebase/v1/mqlread");
url.put("query", mqlQuery);
url.put("key", apikey);
url.put("cursor", cursor);
logger.debug("QUERY URL: " + url.toString());
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject) parser.parse(httpResponse
.parseAsString());
return response;
}
代码示例来源:origin: edu.illinois.cs.cogcomp/big-data-utils
public JSONObject getCursorAndResponse(String mqlQuery, String cursor)
throws IOException, ParseException {
HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport
.createRequestFactory();
JSONParser parser = new JSONParser();
GenericUrl url = new GenericUrl(
"https://www.googleapis.com/freebase/v1/mqlread");
url.put("query", mqlQuery);
url.put("key", apikey);
url.put("cursor", cursor);
logger.debug("QUERY URL: " + url.toString());
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject) parser.parse(httpResponse
.parseAsString());
return response;
}
代码示例来源:origin: stackoverflow.com
HttpTransport transport = new UrlFetchTransport();
// add default headers
GoogleHeaders defaultHeaders = new GoogleHeaders();
transport.defaultHeaders = defaultHeaders;
transport.defaultHeaders.put("Content-Type", "application/json");
JsonCParser parser = new JsonCParser();
parser.jsonFactory = new JacksonFactory();
transport.addParser(parser);
// build the HTTP GET request and URL
HttpRequest request = transport.buildGetRequest();
request.setUrl("https://maps.googleapis.com/maps/api/place/details/json?reference=CmRYAAAAciqGsTRX1mXRvuXSH2ErwW-jCINE1aLiwP64MCWDN5vkXvXoQGPKldMfmdGyqWSpm7BEYCgDm-iv7Kc2PF7QA7brMAwBbAcqMr5i1f4PwTpaovIZjysCEZTry8Ez30wpEhCNCXpynextCld2EBsDkRKsGhSLayuRyFsex6JA6NPh9dyupoTH3g&sensor=true&key=<APIKEY>");
try {
HttpResponse response = request.execute();
String r = response.parseAsString();
代码示例来源:origin: com.google.cloud/google-cloud-core
private static String getAppEngineProjectIdFromMetadataServer() throws IOException {
String metadata = "http://metadata.google.internal";
String projectIdURL = "/computeMetadata/v1/project/project-id";
GenericUrl url = new GenericUrl(metadata + projectIdURL);
HttpTransport netHttpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory();
HttpRequest request =
requestFactory
.buildGetRequest(url)
.setConnectTimeout(500)
.setReadTimeout(500)
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
HttpResponse response = request.execute();
return headerContainsMetadataFlavor(response) ? response.parseAsString() : null;
}
内容来源于网络,如有侵权,请联系作者删除!