程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从服务器下载JSON的示例?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何从服务器下载JSON的示例??

开发过程中遇到如何从服务器下载JSON的示例?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何从服务器下载JSON的示例?的解决方法建议,希望对你解决如何从服务器下载JSON的示例?有所启发或帮助;

这应该可以解决问题

String JsonResponse = httpHelper.connect(SERVER_URL);
JsONObject Json=new JsONObject(JsonResponsE);

private static String convertStreamToString(inputStream is) {
        /*
         * To convert the inputStream to String we use the BufferedReader.readline()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new inputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readline()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException E) {
            e.printstacktrace();
        } finally {
            try {
                is.close();
            } catch (IOException E) {
                e.printstacktrace();
            }
        }
        return sb.toString();
    }
    public static String connect(String url)
    {
        httpClIEnt httpclIEnt = new DefaulthttpClIEnt();
        httpGet httpget = new httpGet(url); 
        httpResponse response;
        try {
            response = httpclIEnt.execute(httpget);
            //Log.i(tag,response.getStatusline().toString());
            httpentity entity = response.getEntity();
            if (entity != null) {
                inputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                instream.close();
                return result;
            }
        } catch (ClIEntProtocolException E) {
        } catch (IOException E) {
        }
        return null;
    }

解决方法

是否有一个很好的示例显示如何查询服务器并下载响应(JSON或XML)?

大佬总结

以上是大佬教程为你收集整理的如何从服务器下载JSON的示例?全部内容,希望文章能够帮你解决如何从服务器下载JSON的示例?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: