方法抛出'java.lang.NullPointerException'异常。无法评估com.android.okhttp.Headers.toString()

时间:2018-04-29 01:08:43

标签: java android android-studio

OS:Mac + android-studio

  1. httpsURLConnection.connect()无法评估com.android.okhttp.Headers.toString()

    package com.mar.android.photogallery;

    导入android.net.Uri; import android.util.Log;

    import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.security.cert.X509Certificate;

    import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager;

    import org.apache.commons.lang3.StringUtils;

    / **

    • 2018/4/9由marares创建。 * /

    public class FlickrFetchr {     private static final String TAG =“FlickrFetchr”;     private static final String API_KEY =“****”;

    public void fetchItems() {
        HttpsURLConnection httpsURLConnection = null;
        BufferedReader reader = null;
        try {
            URL url = new URL("https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=***&format=json&nojsoncallback=1&extras=url_s");
            System.out.println("1====================");
            httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setConnectTimeout(5000);
            httpsURLConnection.setDoInput(true);
            httpsURLConnection.setUseCaches(false);
            System.out.println("2====================");
    
            httpsURLConnection.connect();//<----Cannot evaluate com.android.okhttp.Headers.toString()
            System.out.println("3====================");
            System.out.println(httpsURLConnection.getResponseCode());
            System.exit(0);
    
            reader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
            StringBuilder sBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sBuilder.append(line);
            }
            Log.e("TAG", "content=" + sBuilder.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpsURLConnection != null) {
                httpsURLConnection.disconnect();
            }
    
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    

    }

0 个答案:

没有答案