我试图连接到Android应用程序中的阳光基础api,但应用程序不断崩溃

时间:2015-05-08 17:31:15

标签: android api http

鉴于邮政编码,我正在尝试使用阳光粉底api将邮政编码的代表送回国会。现在,oncreate方法应该打印出邮政编码。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String zipcode = intent.getStringExtra(GetZipCode.EXTRA_MESSAGE);
        try {
            printCongressmen(zipcode);
        }
        catch(MalformedURLException e){

        }
        catch(IOException ex){

        }
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(zipcode);
        setContentView(textView);
    }    

但是,在运行printCongressmen方法后崩溃:

public void printCongressmen(String zipcode) throws IOException {
    String apikey = "6562c36e87ba41f6bc887104d1e82eb8";
    String baseURL = "https://congress.api.sunlightfoundation.com";
    String zipCodeAddition = "/legislators/locate?apikey="+apikey + "&zip=" + zipcode;
    String url = baseURL + zipCodeAddition;
    URL apiurl = new URL(url);
    HttpsURLConnection urlConnection = (HttpsURLConnection) apiurl.openConnection();
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        int data = in.read();
        Log.v("INPUTSTREAM DATA", String.valueOf((char) data));
    }
    finally {
        urlConnection.disconnect();
    }
}

很抱歉,如果我做了一些明显错误的事情,我是Android的新手并且一直在研究连接到网址,但我无法让它工作。该应用程序只是关闭,而不会显示邮政编码或记录任何东西到logcat。

0 个答案:

没有答案