本文整理汇总了Java中spark.utils.IOUtils.toString方法的典型用法代码示例。如果您正苦于以下问题:Java IOUtils.toString方法的具体用法?Java IOUtils.toString怎么用?Java IOUtils.toString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spark.utils.IOUtils
的用法示例。
在下文中一共展示了IOUtils.toString方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildByLatLong
import spark.utils.IOUtils; //导入方法依赖的package包/类
/**
* Build City information by latitude and longitude
* @param latlong [latitude,longitude]
*/
public void buildByLatLong(String[] latlong){
try{
//query attributes for openstreetmap
//format = json -> response should be in json
//lat -> set latitude
//lon -> set longitude
//limit = 1 -> only want the most relevant location
//addressdetails = 1 -> request detailed information
//country = us -> only look in the US
String query = String.format("format=json&lat=%s&lon=%s&limit=1&addressdetails=1&country=us", URLEncoder.encode(latlong[0], charset), URLEncoder.encode(latlong[1], charset));
URLConnection connection = new URL(reverse_url + "?" + query).openConnection(); //create request
connection.setRequestProperty("Accepted-Charset", charset); //make sure we received the correct charset
InputStream response = connection.getInputStream(); //receive input as input stream
String in = IOUtils.toString(response); //turn response to string
JsonObject jobject = new JsonParser().parse(in).getAsJsonObject(); //use gson to parse string response
JsonObject address = jobject.getAsJsonObject("address");
zip = address.get("postcode").getAsString(); //store zip code
name = address.get("city").getAsString(); //store city name
state = address.get("state").getAsString(); //store state name
}
catch (Exception e){ //catch errors
e.printStackTrace();
}
}
示例2: request
import spark.utils.IOUtils; //导入方法依赖的package包/类
private TestResponse request(String method, String path) {
try {
// FIXME this should work in a CI environment if possible
URL url = new URL("http://localhost:4567" + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
String body = IOUtils.toString(connection.getInputStream());
return new TestResponse(connection.getResponseCode(), body);
} catch (IOException e) {
e.printStackTrace();
fail("Sending request failed: " + e.getMessage());
return null;
}
}
示例3: fileContent
import spark.utils.IOUtils; //导入方法依赖的package包/类
@VisibleForTesting
static String fileContent(String location) {
try {
InputStream reader = Viewer.class.getResourceAsStream(location);
return IOUtils.toString(reader);
} catch (Exception e) {
LOGGER.error("Unable to read file at location: " + location + ".", e);
}
return "// Unable to read file at location:\n// \"" + location + "\"\n";
}
示例4: buildByZip
import spark.utils.IOUtils; //导入方法依赖的package包/类
/**
* Build City Information by Zip Code
* @param zip_code Zip Code of location
*/
public void buildByZip(String zip_code){
try{
//query attributes for openstreetmap
//postalcode -> zipcode to search for
//countrycodes = us -> only look in the US
//limit = 1 -> only want the most relevant location
//addressdetails = 1 -> request detailed information
//format = json -> response should be in json
//polygon_geojson = 1 -> for extensibility if we want to make polygons from json
//country = us -> only look in the US
String query = String.format("postalcode=%s&countrycodes=us&limit=1&addressdetails=1&format=json&polygon_geojson=1&country=us",URLEncoder.encode(zip_code, charset));
URLConnection connection = new URL(search_url + "?" + query).openConnection(); //create request
connection.setRequestProperty("Accepted-Charset", charset); //make sure we received the correct charset
InputStream response = connection.getInputStream(); //receive input as input stream
String in = IOUtils.toString(response); //turn response to string
JsonArray jarray = (JsonArray) new JsonParser().parse(in); //use gson to parse string response
JsonObject jobject = jarray.get(0).getAsJsonObject(); //get the first element (most relevant & should be only element because limit=1)
String[] res = {jobject.get("lat").getAsString(), jobject.get("lon").getAsString()}; //parse latitude and longitude into a string array
//System.out.println("Found lat: " + res[0] + " and lon: " + res[1] + " from zip code: " + zip_code);
lat = res[0]; //store lat
lon = res[1]; //store lon
JsonObject address = jobject.getAsJsonObject("address");
name = address.get("city").getAsString(); //store city name
state = address.get("state").getAsString(); //store state name
}
catch (Exception e){ //catch errors
e.printStackTrace();
}
}
示例5: buildByCityState
import spark.utils.IOUtils; //导入方法依赖的package包/类
/**
* Build City information by city name and city state
* @param city_name Name of city
* @param city_state Name of state
*/
public void buildByCityState(String city_name, String city_state){
try{
//query attributes for openstreetmap
//format = json -> response should be in json
//city -> name of city
//state -> name of state
//limit = 1 -> only want the most relevant location
//addressdetails = 1 -> request detailed information
//country = us -> only look in the US
String query = String.format("city=%s&state=%s&limit=1&addressdetails=1&format=json&country=us", URLEncoder.encode(city_name, charset), URLEncoder.encode(city_state, charset));
URLConnection connection = new URL(search_url + "?" + query).openConnection(); //create request
connection.setRequestProperty("Accepted-Charset", charset); //make sure we received the correct charset
InputStream response = connection.getInputStream(); //receive input as input stream
String in = IOUtils.toString(response); //turn response to string
JsonArray jarray = (JsonArray) new JsonParser().parse(in); //use gson to parse string response
JsonObject jobject = jarray.get(0).getAsJsonObject();
lat = jobject.get("lat").getAsString(); //store lat
lon = jobject.get("lon").getAsString(); //store lon
//JsonObject address = jobject.getAsJsonObject("address");
//name = address.get("city").getAsString();//store city
//state = address.get("state").getAsString();//store state
}
catch (Exception e){ //catch errors
e.printStackTrace();
}
}
示例6: executeRequest
import spark.utils.IOUtils; //导入方法依赖的package包/类
private static String executeRequest(String method, String path) throws IOException {
URL url = new URL("http://localhost:8080" + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
return IOUtils.toString(connection.getInputStream());
}
示例7: read
import spark.utils.IOUtils; //导入方法依赖的package包/类
private String read(String subPath) {
try {
return IOUtils.toString(new URI("http://localhost:8501/"+subPath).toURL().openStream());
} catch (IOException | URISyntaxException e) {
throw new RuntimeException("Got exception", e);
}
}
示例8: request
import spark.utils.IOUtils; //导入方法依赖的package包/类
protected TestResponse request(String method, String path, String body,boolean isByteArray)
{
try
{
URL url = new URL("http://localhost:4567" + path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
//connection.setRequestProperty("Content-Type","application/json");
if(body != null)
{
byte[] outputInBytes = body.getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write( outputInBytes );
os.close();
}
if(isByteArray)
{
byte[] responseBodyByteArray = IOUtils.toByteArray(connection.getInputStream());
return new TestResponse(connection.getResponseCode(), responseBodyByteArray);
}
else
{
String responseBody = IOUtils.toString(connection.getInputStream());
return new TestResponse(connection.getResponseCode(), responseBody);
}
} catch (IOException e)
{
e.printStackTrace();
fail("Sending request failed: " + e.getMessage());
return null;
}
}