mardi 13 mars 2018

how did i can use a listener to my code to prevent a FATAL EXCEPTION

this section (HTTPUtils Class) of code i use it to get the data from the internet , the problem is when i run the app(in android studio) HTTPUtils class run the thread and go back to the main , in this time the procces trying to continuo the code , but the thread in HTTP.. still workin so we got FATAL EXCEPTION , they told me to use listener to slove the problem i don't know how to add listener for this code .

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HTTPUtils {
    public static String readIn;

    public HTTPUtils() {
    }

    public void read(String url) throws Exception{


            new Thread(() -> {
                    URL address = null;
                    try {
                        address = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
                    try {
        HttpURLConnection con = (HttpURLConnection) address.openConnection();
        con.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
        int responseCode = con.getResponseCode();
        InputStream in = con.getInputStream();

        readIn = StreamIO.read(in);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
                //WHY the casting?
                //  int responseCode = con.getResponseCode(); //we want it to be OK == 200


                //con.disconnect();


            ).start();

    }
    public static String getReadIn(){
        return readIn;
    }
}


package com.orwa;

import com.google.gson.Gson;
import com.orwa.weather.models.APIres;
import com.orwa.weather.utils.HTTPUtils;

/**
 * Created by User on 10/01/2018.
 */

public class ExtractionData  {
    static APIres apIres;
    ExtractionData(String msg) {
        try {
            String start = "http://api.openweathermap.org/data/2.5/forecast?appid=0cd0b52c786c9ee6e1ce3a25f701d14d&q=";
            //String city = msg;
            String city = "arara";
            String end = ",il&units=metric";
            String url = start + city + end;
            HTTPUtils httpUtils =  new HTTPUtils();
            httpUtils.read(url);
            String jeson = HTTPUtils.getReadIn();
            Gson geson = new Gson();

            apIres = geson.fromJson(jeson,  APIres.class);
            //Rain apIres2= geson.fromJson(jeson, Rain.class);
            //System.out.println("*******************"+apIres.getCity().getName());
            //apIres2.get3h();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static public APIres getApiRes(){
        return apIres;
    }

}

in the main

        new ExtractionData("arara");
        APIres apIres =ExtractionData.getApiRes();

Aucun commentaire:

Enregistrer un commentaire