Java – I get an IOException error and try-catch does not save me

I get an IOException error that says:

Unreported exception IOException, must be intercepted or declared to be dropped

I was looking for answers, but none of them were helpful. That is why I asked. The debugger displays this line:

public static article Article = new article ();

Just the right side gives mistakes. I was trying to use try catch, but it only resulted in more errors. I also tried to take this line within the menu class, but it always comes back to mistakes. I'm pretty sure I'm missing something that's so simple, but I really want to ask you.

package proje;

import java.util.ArrayList;
Import java.util.Scanner;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class home extends item {
static ArrayList Cart = new ArrayList <> ();

public static article Article = new article ();
static int id;
static scanner scan = new scanner (System.in);

public static void menu (string type, string username) {
Scanner input = new scanner (System.in);

if (type.equals ("admin") || type.equals ("admin")) {

System.out.println ("********** ADMIN *********");
System.out.println ("0 - LOGOUT");
System.out.println ("1 - ADD PRODUCT");
System.out.println ("2 - REMOVE PRODUCT");
System.out.println ("3 - LIST PRODUCT");
System.out.println ("4 - EXIT");

int slc = input.nextInt ();

Switch (slc) {
Case 0:
Proje.entry ();
break;
Case 1:
System.out.println ("Product Type:");
String prdType = input.next ();

System.out.println ("product name:");
String prdName = input.next ();

System.out.println ("Product Price:");
double prdPrice = input.nextDouble ();

System.out.println ("Quantity:");
int prdQuantity = input.nextInt ();

System.out.println ("Product Description:");
String prdDescription = input.next ();

checkID ();

Item tempNewItem = new Item (id, prdName, prdPrice, prdDescription, prdQuantity, prdType);
item.addItemToList (tempNewItem);

System.out.println ("You have added an article!");
String str;
BufferedOutputStream b = null;

To attempt {
BufferedOutputStream b2 = new BufferedOutputStream (new FileOutputStream ("products.txt", true));
str = String.valueOf (tempNewItem);
byte[] byteCal = str.getBytes ();
b2.write (byteCal);
b2.flush ();
b2.close ();
}
catch (IOException e) {
e.printStackTrace (); // unnecessary
}
Home.Menu ("Admin", username);
Case 2:
for (Article i: itemList) {
System.out.println ("productname:" + i.getName ());
}
System.out.println ("Which product will be removed:");
String removeItem = input.next ();
item.removeItem (removeItem);
System.out.println (removeItem + "will be removed!"););
Home.Menu ("Admin", username);
Case 3:
printIDs ();
System.out.println ("ID:");
int selectionId = input.nextInt ();
item.getItemDescription (selectionId);
Home.Menu ("Admin", username);
Case 4:
System.exit (4);
break;
Default:
break;
}
}
else if (type.equals ("user") || type.equals ("user")) {
System.out.println ("********* USER MENU *********");
System.out.println ("0 - LOGOUT");
System.out.println ("1 - LIST PRODUCTS");
System.out.println ("2 - LIST PRODUCT TYPES");
System.out.println ("3 - DESCRIPTION OF PRODUCTS");
System.out.println ("4 - ADD TO BASKET");
System.out.println ("5 - REMOVE SHOPPING CART");
System.out.println ("6 - HISTORY");
System.out.println ("7 - LIST ITEMS");
System.out.println ("8 -> EXIT");

int selection = input.nextInt ();

Switch (selection) {
Case 0:
Proje.entry ();
System.out.println ("-------------");
Menu ("User", Username);
Case 1:
item.getItemList ();
System.out.println ("-------------");
Menu ("User", Username);
Case 2:
item.ListProductOfType ();
System.out.println ("-------------");
Menu ("User", Username);
Case 3:
printIDs ();
System.out.println ("Product ID:");

int selectionId = input.nextInt ();
item.getItemDescription (selectionId);
System.out.println ("-------------");
Menu ("User", Username);
Case 4:
item.getItemList ();
System.out.println (("elementname:"));
String optionName = input.next ();
for (Article i: itemList) {
if (i.getName (). equals (optionname)) {
Cart.add (i);
}
}
for (article i: shopping cart) {
System.out.println (i.getName () + "has been added to your cart!");
}
System.out.println ("-------------");
Menu ("User", Username);
Case 5:
if (Cart.size () == 0) {
System.out.println ("No item in shopping cart!");
Menu ("User", Username);
}
miscellaneous
System.out.println ("product name:");
String selectionName = input.next ();

for (int i = 0; i <Cart.size (); i ++) {
if (Cart.get (i) .getName (). equals (selectionName)) {
Cart.remove (i);
}
}
for (article i: shopping cart) {
System.out.println (i.getName ());
}

System.out.println (selectionName + "is canceled!");
System.out.println ("-------------");
}
Menu ("User", Username);
Case 6:
System.out.println ("History could not be loaded!");
System.out.println ("-------------");
Menu ("User", Username);
Case 7:
System.out.println ("Product Type:");
String selectType = input.next ();
item.getItemType (selectType);
System.out.println ("-------------");
Menu ("User", Username);
Case 8:
System.exit (8);
break;
Default:
break;
}
}
miscellaneous
System.out.println ("Invalid input!");
}
}
public static void printIDs () {
for (Article i: itemList) {
System.out.println ("Article ID:" + i.getID () + "/" + "Product Name:" + i.getName ());
}
}
public static int checkID () {
System.out.println ("ID:");
int id = scan.nextInt ();
for (Item temp: itemList) {
if (temp.getID () == id) {
printIDs ();
System.out.println ("ID already exists!  NID:");
checkID ();
}
}
Return ID;
}
}

java – OpenStream throws IOException in AsyncTask

I'm developing a weather application for a course. This application uses an asynchronous task to get the temperature of a city from the OpenWeather API. The API returns a JSON with weather information via a URL like the following:

http://api.openweathermap.org/data/2.5/weather?q={city name} & appid = {key}

APPID is a key associated with an account in OpenWeather. Here is a JSON example for Berlin City:

{"coord": {"lon": 13.39, "lat": 52.52}, "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}], "Base": "Stations", "Main": {"Temp": 8.49, "Pressure": 1035, "Humidity": 61, "Temp_min": 8, "Temp_max": 9}, "Visibility" : 10000, "wind": {"speed": 6.7, "deg": 120}, "clouds": {"all": 0}, "dt": 1542369000, "sys": {"type": 1, id: 4892, message: 0.0027, country: DE, sunrise: 1542349837, sunset: 1542381079}, id: 2950159, name: Berlin, cod ": 200}

If the city does not exist, the API returns a JSON with error 404. The next one is a JSON example for the nonexistent city Berlina,

{"cod": "404", "message": "city not found"}

I get the JSON with Asynctask. The code for the doInBackground () method is next.

@Run over
protected String doInBackground (String ... URLs) {
InputStream inputStream;
String result = ""; // Since un resultado is si si inicializa con zero!

To attempt {
Log.d ("Flag", "1");

inputStream = new URL (URLs)[0]) .openStream ();
Log.d ("Flag", "2");

if (inputStream! = null) {
Log.d ("Flag", "3");
BufferedReader buffer = new BufferedReader (new InputStreamReader (inputStream));
String line;

Log.d ("Flag", "4");

while ((line = buffer.readLine ())! = null) {
Result + = line;
}
Log.d ("Flag", "5");

inputStream.close ();
}
miscellaneous
// Error
}
}
catch (MalformedURLException e) {
Log.d ("MalformedURL", e.getLocalizedMessage ());
}
catch (IOException e) {
Log.d ("IOException", e.getLocalizedMessage ());
}
catch (exception e) {
Log.d ("InputStream", e.getLocalizedMessage ());

}

Return result;
}

So I analyze the JSON in postOnExecute () and display the temperature information with a toast.

My problem is this: The code works well with Berlin and throw IOException for Berlina, I analyzed the logcat and saw that the problem is in openStream (), but I can not say why it fails. Could it be a limitation of the API? Why can I get the answer for Berlin correctly?

I've read the reference from Android (https://developer.android.com/reference/java/net/URL), for which openStream is an abbreviation for:

    openConnection (). getInputStream ()

Could the openConnection () method fail? What could be the reason?