无法在Android上检索JSON

时间:2015-02-18 10:26:56

标签: android json parsing httpconnection

我正在尝试从OpenWeatherMap api下载天气数据。有很多这方面的教程,在实践中看起来很简单,但我无法让它工作!

如果我到了网址Click Here

你可以看到JSON已经形成..

使用我的代码:

public class RemoteFetch {

private static String BASE_URL = "http://api.openweathermap.org/data/2.5/weather?q=";




   public static JSONObject getJSON(String city) {

        HttpURLConnection con = null;
        InputStream is = null;

        try {
            con = (HttpURLConnection) (new URL(BASE_URL + city)).openConnection();
            con.setRequestMethod("GET");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.connect();

            // Let's read the response
            StringBuffer buffer = new StringBuffer();
            is = con.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = br.readLine()) != null)
                buffer.append(line + "\r\n");

            is.close();
            con.disconnect();
            String json = buffer.toString();
            Log.e("JSON", "value is " + json.toString());
            JSONObject data = new JSONObject(json);
            Log.e("JSON", "Value of request is " + data.getInt("cod"));


            // This value will be 404 if the request was not
            // successful
            if (data.getInt("cod") != 200) {
                return null;
            }

            return data;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                is.close();
            } catch (Throwable t) {
            }
            try {
                con.disconnect();
            } catch (Throwable t) {
            }
        }

    }
}

我什么都没收到,行总是空的。

4 个答案:

答案 0 :(得分:0)

也许您想学习如何使用http连接自己获取JSON,但如果没有,您可以更轻松地使用像volley这样的库:http://developer.android.com/training/volley/request.html#request-json

答案 1 :(得分:0)

尝试使用此方法从服务器获取响应。

private static String BASE_URL = "http://api.openweathermap.org/data/2.5/weather?q=";


public String getInternetData(String city) throws Exception {


        BufferedReader in = null;
        String data = null;

        try {
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(),
                    3000);

            URI website = new URL(BASE_URL + city)
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            response.getStatusLine().getStatusCode();

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            //String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l);
            }
            in.close();
            data = sb.toString();
            return data;
        } finally {
            if (in != null) {
                try {
                    in.close();
                    return data;
                } catch (Exception e) {
                    Log.e("GetMethodEx", e.getMessage());
                }
            }
        }
    }

您必须将android.permission.INTERNET权限添加到您应用的清单

答案 2 :(得分:0)

setDoOutput()仅在您想要将数据发布到服务器时使用。虽然您将该方法称为get.This chnages it to post.Please检查setDoOuput()和setDoInput()的定义和用例相应地使用。

答案 3 :(得分:0)

我想这会对你有帮助,

使用getter和setters方法创建一个类,例如

class A
{
int b;
void setb(int x){this.b = x;}
int getb(){return this.b}
}

比你可以从这个类的对象创建json:

new Gson().toJson(a)

来自json的对象:

a = new Gson().fromJson(data, A.class);