Java – try the method again for some exceptions

I have the following methods to retrieve information from the server using a proxy Sometimes due to proxy errors, I get socketexception, sslexexception, sslhandshakeexception or connectexception

As you can see in my method, I'm already using catch (IOException IOE). I need to do this to get the content of the server response when the server returns anything except code 200

If the above exception occurs, how to reset the method?

public String getMeta() throws IOException
{

    HttpsURLConnection con = null;
    InputStream is = null;
    StringWriter writer = new StringWriter();
    try
    {
        String url = "https://api.myapp.com/Meta";
        URL urlObj = new URL(url);

        if (useProxy && fullProxy)
        {
            myapp.Proxy proxyCustom = getRandomProxy();
            Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyCustom.getProxyIp(),proxyCustom.getProxyPort()));
            con = (HttpsURLConnection) urlObj.openConnection(proxy);
        }
        else
        {
            con = (HttpsURLConnection) urlObj.openConnection();
        }

        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent",USER_AGENT);
        con.setRequestProperty("Content-Type","application/json; charset=utf-8");
        con.setRequestProperty("host",urlObj.getHost());
        con.setRequestProperty("Connection","Keep-Alive");

        int responseCode = 0;

        responseCode = con.getResponseCode();

        is = con.getInputStream();
        writer = new StringWriter();
        IoUtils.copy(is,writer,"UTF-8");
    }
    catch (IOException ioe)
    {
        if (con instanceof HttpsURLConnection)
        {
            HttpsURLConnection httpConn = (HttpsURLConnection) con;
            int statusCode = httpConn.getResponseCode();
            if (statusCode != 200)
            {
                is = httpConn.getErrorStream();
                writer = new StringWriter();
                IoUtils.copy(is,"UTF-8");
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return writer.toString();
}

Solution

One method shown below is to make the getmeta method actually throw IOException Then, due to any caught exception, you can call yourself recursively using the calling method

In order to be able to call the method multiple times, the number of passes is taken as a parameter and the stop standard logic is processed accordingly For example:

public String caller(int total) throws IOException{
    return callerImpl(1,total);
}

public String callerImpl(int current,int total) throws IOException{
    try{
        return getMeta();
    }catch(IOException e){
        current++;
        if ( current > total ){
            throw e;//or return null or empty string,depending upon upstream requirements
        }
        return callerImpl(current,total);
    }

    return null;
}

In getmeta:

try{
    ....
}catch(IOException io){
    //log or handle io
    throw io;
}

Note that there is no log / logic for throwing exceptions above, and you may want to handle them in some way

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>