如何使用HttpURLConnection的,而不是凌空获得JSON对象?

编程入门 行业动态 更新时间:2024-10-18 12:22:44
本文介绍了如何使用HttpURLConnection的,而不是凌空获得JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要做的就是一些数据从一个的的HttpURLConnection 而不是使用 JsonObjectRequest()从凌空库。

What I'm trying to do is get some data from a HttpURLConnection instead of using JsonObjectRequest() from the Volley library.

下面是我用它来从我的服务器抓取一个JSON对象的code。

Below is the code that I use to grab a JSON object from my server.

JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,"myURL", null, new Response.Listener<JSONObject>();

我试图改变空到的JSONObject 在 myURL 改为空。但是,这并没有工作了。

I tried changing the null to a JSONObject after myURL was changed to null. But that did not work out.

推荐答案

在 JsonObjectRequest网址()不可选的,JSONObject的参数是用来发布的参数与请求的URL。

The url in JsonObjectRequest() is not optional, and the JSONObject parameter is used to post parameters with the request to the url.

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

developer.android/training/volley/index.html

JsonObjectRequest

JsonObjectRequest

公共JsonObjectRequest(INT方法,                            字符串URL,                            的JSONObject jsonRequest,                            Response.Listener监听器,                            Response.ErrorListener errorListener)创建一个新的请求。

public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) Creates a new request.

参数:

方法 - HTTP方法使用

method - the HTTP method to use

URL - URL从

url - URL to fetch the JSON from

jsonRequest - 一个JSONObject的发布与请求。空是允许的   并表示没有参数,将张贴与要求。

jsonRequest - A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.

监听器 - 监听器接收JSON响应

listener - Listener to receive the JSON response

errorListener - 错误监听,或者为null,忽略错误

errorListener - Error listener, or null to ignore errors.

使用的HttpURLConnection :

developer.android/reference/java/net/HttpURLConnection.html

在code将是这样的:

The code would be something like this:

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("api.github/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 } }

更多推荐

如何使用HttpURLConnection的,而不是凌空获得JSON对象?

本文发布于:2023-07-14 19:53:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   而不是   对象   HttpURLConnection   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!