android:无法解析符号“urls”

时间:2015-11-30 04:50:28

标签: android androidhttpclient android-json

我是android新手编程,我正在关注youtube视频中的代码,但我收到错误“无法解析符号”urls“”for“HttpGet httppost = new HttpGet(urls [0]);”。我通过研究发现,在Android中不再支持HttpGet是否有任何解决方法

    @Override
protected Boolean doInBackground(String... params) {
    try {

        HttpGet httppost = new HttpGet(urls[0]);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse respone = httpclient.execute(httppost);

        int status = respone.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity entity = respone.getEntity();
            String data = EntityUtils.toString(entity);

            JSONObject jsono = new JSONObject(data);
            JSONArray jarray = jsono.getJSONArray("forecast");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                Forecast forecast = new Forecast();

                forecast.setCode(object.getString("code"));
                forecast.setDate(object.getString("date"));
                forecast.setDay(object.getString("day"));
                forecast.setHigh(object.getString("high"));
                forecast.setLow(object.getString("low"));
                forecast.setText(object.getString("text"));

                forecastList.add(forecast);


            }
            return true;
        }


    } catch (ParseException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }


    return false;
}

2 个答案:

答案 0 :(得分:2)

如果您之前未在代码中定义网址,只需将代码中的网址替换为params 即可。

答案 1 :(得分:1)

您必须将doInBackground方法的参数名称更改为url。你接收它作为params,这就是为什么它告诉url没有定义。

 @Override
protected Boolean doInBackground(String... urls) {

}