栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用HttpURLConnection而不是Volley获取JSON对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用HttpURLConnection而不是Volley获取JSON对象?

中的url

JsonObjectRequest()
不是可选的,并且JSONObject参数用于将带有请求的参数发布到url。

从文档中:http
:
//afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html

http://developer.android.com/training/volley/index.html

JsonObjectRequest

public JsonObjectRequest(int方法,字符串url,JSONObject
jsonRequest,Response.Listener侦听器,Response.ErrorListener
errorListener)创建一个新请求。

参数:

method-要使用的HTTP方法

url-从中获取JSON的URL

jsonRequest-与请求一起发布的JSONObject。允许为空,表示没有参数将与请求一起发布。

侦听器-侦听器以接收JSON响应

errorListener-错误侦听器,如果为null则忽略错误。

使用

HttpURLConnection

http://developer.android.com/reference/java/net/HttpURLConnection.html

该代码将是这样的:

 public class getData extends AsyncTask<String, String, String> {        HttpURLConnection urlConnection;        @Override        protected String doInBackground(String... args) { StringBuilder result = new StringBuilder(); try {     URL url = new URL("https://api.github.com/users/dmnugent80/repos");     urlConnection = (HttpURLConnection) url.openConnection();     InputStream in = new BufferedInputStream(urlConnection.getInputStream());     BufferedReader reader = new BufferedReader(new InputStreamReader(in));     String line;     while ((line = reader.readLine()) != null) {         result.append(line);     } }catch( Exception e) {     e.printStackTrace(); } finally {     urlConnection.disconnect(); } return result.toString();        }        @Override        protected void onPostExecute(String result) { //Do something with the JSON string        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号