HttpURLConnection+JSON的使用实例

JSON

JSON是一种轻量级的数据交换格式,主要用于跟服务器进行交换数据。
JSON数据格式应用非常广泛,在APP开发中,大多数据端返回的接口数据都是采用JSON数据格式的。

JSON的两种数据格式

JSONObject和JSONArray,JSONObject表示JSON对象,而JSONArray表示的则是数组。

运用HttpURLConnection+JSON获取天气预报数据信息

代码如下
Activity中xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.lenovo.liu.weatherdemo.MainActivity">

    <EditText
        android:id="@+id/weath_city"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="请输入所要查询的城市" />

    <Button
        android:id="@+id/weather_btn"
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:text="查询" />

    <TextView
        
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中使用JSON进行POST请求通信,可以使用HttpURLConnection或HttpClient来实现。以下是一个简单的示例,展示如何封装一个POST请求通信模块。 1. 首先,创建一个名为HttpUtils的类,并将其声明为单例模式,以便在整个应用程序中共享。 ```java public class HttpUtils { private static HttpUtils instance; private HttpUtils() {} public static synchronized HttpUtils getInstance() { if (instance == null) { instance = new HttpUtils(); } return instance; } // 实现发送POST请求的方法 public void sendPostRequest(String url, JSONObject postData, final OnResponseListener listener) { new Thread(new Runnable() { @Override public void run() { try { // 创建连接和设置请求参数 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(5000); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); // 发送数据 OutputStream outputStream = connection.getOutputStream(); outputStream.write(postData.toString().getBytes()); outputStream.flush(); outputStream.close(); // 获取响应数据 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); inputStream.close(); // 回调响应结果 listener.onResponse(response.toString()); } else { listener.onError("请求失败:" + responseCode); } } catch (Exception e) { e.printStackTrace(); listener.onError("请求失败:" + e.getMessage()); } } }).start(); } } ``` 2. 创建一个名为OnResponseListener的接口,用于回调请求的响应结果。 ```java public interface OnResponseListener { void onResponse(String response); void onError(String error); } ``` 3. 在使用该模块时,可以调用sendPostRequest方法发送POST请求,并传入请求URL、JSON数据和OnResponseListener回调对象。 ```java HttpUtils.getInstance().sendPostRequest("http://example.com/api", jsonData, new OnResponseListener() { @Override public void onResponse(String response) { // 处理请求成功的响应结果 } @Override public void onError(String error) { // 处理请求失败的情况 } }); ``` 通过这个模块封装实例,可以方便地发送JSON格式的POST请求,并根据请求结果进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值