Android前端连接web后端的问题,JSON数据传不过来

这是web服务端的数据图片说明

下面是安卓端代码:图片说明
图片说明

小弟不胜感激

http://blog.csdn.net/runner__1/article/details/53365412

亲测有效,希望可以帮到你

HttpClient不是已经不支持了吗?现在基本上要么用HttpURLClient或者OKhttp

不要在主线程直接发起网络调用这种耗时操作。你不觉得你的手一直在卡者的么。。将getContent放在一个新启的子线程中执行,再看看。

public static URL buildUrl(String locationQuery) {
Uri builtUri = Uri.parse("https://andfun-weather.udacity.com/staticweather").buildUpon()
.appendQueryParameter("q", locationQuery)
.appendQueryParameter("mode", "json")
.appendQueryParameter("units", "metric")
.appendQueryParameter("cnt", "14")
.build();

    URL url = null;
    try {
        url = new URL(builtUri.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return url;
}

public static String getResponseFromHttpUrl(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);

        StringBuilder response = new StringBuilder();
        String readLine;
        while ((readLine = reader.readLine()) != null) {
            response.append(readLine);
        }

        return response.toString();
    } finally {
        connection.disconnect();
        if (reader != null) {
            reader.close();
        }
    }
}