本文整理匯總了Java中com.google.gson.JsonIOException類的典型用法代碼示例。如果您正苦於以下問題:Java JsonIOException類的具體用法?Java JsonIOException怎麽用?Java JsonIOException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JsonIOException類屬於com.google.gson包,在下文中一共展示了JsonIOException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readGeneric
import com.google.gson.JsonIOException; //導入依賴的package包/類
public static int readGeneric(File f) {
int count = 0;
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Location.class, new LocationAdapter()).create();
try (FileReader reader = new FileReader(f)) {
GenericNPC[] vils = gson.fromJson(reader, GenericNPC[].class);
for (GenericNPC gv : vils) {
if (gv == null)
continue;
NPCManager.register(gv);
genericVillagers.add(gv);
}
count += vils.length;
} catch (JsonSyntaxException | JsonIOException | IOException e) {
System.err.println("Error reading NPC in " + f.getName() + ".");
e.printStackTrace();
}
return count;
}
示例2: handleResponseError
import com.google.gson.JsonIOException; //導入依賴的package包/類
@Override
public void handleResponseError(Context context, Throwable t) {
Timber.tag("Catch-Error").w(t.getMessage());
//這裏不光是隻能打印錯誤,還可以根據不同的錯誤作出不同的邏輯處理
String msg = "未知錯誤";
if (t instanceof UnknownHostException) {
msg = "網絡不可用";
} else if (t instanceof SocketTimeoutException) {
msg = "請求網絡超時";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "數據解析錯誤";
}
ArmsUtils.snackbarText(msg);
}
示例3: writeInternal
import com.google.gson.JsonIOException; //導入依賴的package包/類
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
Charset charset = this.getCharset(outputMessage.getHeaders());
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);
try {
if(this.jsonPrefix != null) {
writer.append(this.jsonPrefix);
}
this.gson.toJson(o, writer);
writer.close();
} catch (JsonIOException var7) {
throw new HttpMessageNotWritableException("Could not write JSON: " + var7.getMessage(), var7);
}
}
示例4: createJsonArrayFromString
import com.google.gson.JsonIOException; //導入依賴的package包/類
/**
* Creates a {@link JsonArray} from the given json string.
*
* @param jsonString the json string to parse
* @return the {@link JsonArray} received while parsing the given json string
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonArray createJsonArrayFromString(final String jsonString) throws JsonParsingFailedException {
InputStream stream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
JsonArray readJsonArray = null;
try {
readJsonArray = new Gson().fromJson(reader, JsonArray.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
}
return readJsonArray;
}
示例5: createJsonObjectFromString
import com.google.gson.JsonIOException; //導入依賴的package包/類
/**
* Creates a {@link JsonObject} from the given json string.
*
* @param jsonString the json string to parse
* @return the {@link JsonObject} received while parsing the given json string
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonObject createJsonObjectFromString(final String jsonString) throws JsonParsingFailedException {
InputStream stream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
JsonObject readJsonObject = null;
try {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
}
return readJsonObject;
}
示例6: createJsonObjectFromFilePath
import com.google.gson.JsonIOException; //導入依賴的package包/類
/**
* Creates a {@link JsonObject} from the json file which can be found at the given path.
*
* Note that this method should only be used with a jsonFilePath starting at folders inside the
* resource directory, e.g. algorithm/baselearner/... . Otherwise it will result in a
* {@link NullPointerException}. For accessing {@link JsonObject}s with a more detailed path, use
* {@link #createJsonObjectFromFile(File)}.
*
* @param jsonFilePath the path of the json file
* @return the {@link JsonObject} received while parsing the json file
* @throws JsonParsingFailedException if any exception occurred at runtime
* @throws NullPointerException if the given file path does not start with a folder or file
* inside the resource directory
*/
public static JsonObject createJsonObjectFromFilePath(final String jsonFilePath) throws JsonParsingFailedException {
JsonObject readJsonObject = null;
InputStream inputStream = JsonUtils.class.getResourceAsStream(jsonFilePath);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
try (Reader reader = new BufferedReader(inputStreamReader)) {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
} catch (FileNotFoundException fileNotFoundException) {
throw new JsonParsingFailedException(fileNotFoundException);
} catch (IOException ioException) {
throw new JsonParsingFailedException(ioException);
}
return readJsonObject;
}
示例7: createJsonObjectFromFile
import com.google.gson.JsonIOException; //導入依賴的package包/類
/**
* Creates a {@link JsonObject} from the given json file.
*
* @param jsonFile the json filet to read
* @return the {@link JsonObject} received while parsing the json file
* @throws JsonParsingFailedException if any exception occurred at runtime
*/
public static JsonObject createJsonObjectFromFile(final File jsonFile) throws JsonParsingFailedException {
JsonObject readJsonObject = null;
try (Reader reader = new FileReader(jsonFile)) {
readJsonObject = new Gson().fromJson(reader, JsonObject.class);
} catch (JsonSyntaxException jsonSyntaxException) {
throw new JsonParsingFailedException(jsonSyntaxException);
} catch (JsonIOException jsonIoException) {
throw new JsonParsingFailedException(jsonIoException);
} catch (FileNotFoundException fileNotFoundException) {
throw new JsonParsingFailedException(fileNotFoundException);
} catch (IOException ioException) {
throw new JsonParsingFailedException(ioException);
}
return readJsonObject;
}
示例8: parse
import com.google.gson.JsonIOException; //導入依賴的package包/類
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonIOException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例9: decode
import com.google.gson.JsonIOException; //導入依賴的package包/類
@Override
public Object decode(Response response, Type type) throws IOException {
if (response.status() == 404) return Util.emptyValueOf(type);
if (response.body() == null) return null;
Reader reader = response.body().asReader();
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
} finally {
ensureClosed(reader);
}
}
示例10: decode
import com.google.gson.JsonIOException; //導入依賴的package包/類
@Override
public Object decode(Response response, Type type) throws IOException {
if (void.class == type || response.body() == null) {
return null;
}
Reader reader = response.body().asReader();
try {
return gson.fromJson(reader, type);
} catch (JsonIOException e) {
if (e.getCause() != null && e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
} finally {
ensureClosed(reader);
}
}
示例11: writeInternal
import com.google.gson.JsonIOException; //導入依賴的package包/類
@Override
protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
Charset charset = getCharset(outputMessage.getHeaders());
try (OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset)) {
if (ignoreType) {
gsonForWriter.toJson(o, writer);
return;
}
if (type != null) {
gsonForWriter.toJson(o, type, writer);
return;
}
gsonForWriter.toJson(o, writer);
} catch (JsonIOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
示例12: parse
import com.google.gson.JsonIOException; //導入依賴的package包/類
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonSyntaxException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例13: fetchUserAccounts
import com.google.gson.JsonIOException; //導入依賴的package包/類
private void fetchUserAccounts() {
NetworkManager.Get(requestUrl, new HttpResponse() {
@Override
public void success(String response) {
try {
Type listType = new TypeToken<ArrayList<NRAccount>>() {
}.getType();
List<NRAccount> accounts = new Gson().fromJson(response, listType);
initRecyclerView(accounts);
}
catch(JsonSyntaxException | JsonIOException jioe) {
Log.i("gson", "parse failed");
}
}
@Override
public void error() {
}
});
}
示例14: handleResponseError
import com.google.gson.JsonIOException; //導入依賴的package包/類
@Override
public void handleResponseError(Context context, Throwable t) {
//Used to provide a monitor for handling all errors
//rxjava need to use ErrorHandleSubscriber (the default implementation of Subscriber's onError method), this monitor to take effect
Timber.tag("Catch-Error").w(t.getMessage());
//Here is not only print errors, but also according to different errors to make different logical processing
String msg = "Unknown";
if (t instanceof UnknownHostException) {
msg = "The network is not available";
} else if (t instanceof SocketTimeoutException) {
msg = "Network timeout";
} else if (t instanceof HttpException) {
HttpException httpException = (HttpException) t;
msg = convertStatusCode(httpException);
} else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
msg = "Data parsing error";
}
UiUtils.snackbarText(msg);
}
示例15: LoadRecipes
import com.google.gson.JsonIOException; //導入依賴的package包/類
public List<Recipe> LoadRecipes() {
Gson gson = new Gson();
List<Recipe> recipes = new ArrayList<>();
for (File file : getRecipesFolder().listFiles()) {
if (!file.getName().endsWith(".json"))
continue;
try {
JsonReader reader = new JsonReader(new FileReader(file));
Recipe recipe = gson.fromJson(reader, Recipe.class);
recipes.add(recipe);
} catch (FileNotFoundException | JsonSyntaxException | JsonIOException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
return recipes;
}