azhuweilong 2020-04-17 21:26 采纳率: 0%
浏览 334

请求天气预报的json数据解析,出错,求大佬解疑答惑,万分感谢

问题描述:写了一个解析网络上天气预报的json数据但是解析不出来,不知道具体出错在那个地方
求大佬解答,万分感谢。
安卓版本是9,api29

出错图片:

图片说明

json数据使用的是气象局提供的免费数据:
图片说明

然后是我用的数据的那一部分:

图片说明

下面是代码:

package com.example.proview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class WeatherJSON extends AppCompatActivity implements View.OnClickListener {
    private Button buttonweather;
    private TextView textView;
    private String Data;
    //请求北京昨天的天气
    private  String url="http://t.weather.sojson.com/api/weather/city/101010100";
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_json);
        buttonweather=(Button)findViewById(R.id.Buttonweather);
        textView=(TextView)findViewById(R.id.textweather);
        buttonweather.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.Buttonweather){
            send();
        }
    }
    //okhttp实现方法
    private void send(){
        //创建OkHttpClient对象
        OkHttpClient okHttpClient=new OkHttpClient();
        Request request=new Request.Builder()
                .url(url)
                .build();
        final Call call=okHttpClient.newCall(request);
        new Thread(new Runnable() {
            @Override
            public void run() {
        try {
            Response response=call.execute();
            String responseData=response.body().string();

            //解析看调用什么方法
            //把请求数据传到pulljson方法
            pulljson(responseData);

        } catch (IOException e) {
            e.printStackTrace();
        }
            }
        }).start();

        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==1){
                    textView.setText(Data);
                }
            }
        };
    }

    //解析json数据
    private void pulljson(String str){
        try {
            //采用系统提供的jsonobject来解析json数据
            JSONObject jsonObject=new JSONObject(str);
            //读取北京市的昨日数据
            JSONObject jsonObject1=jsonObject.getJSONObject("yesterday");
            //获取数据
            String date=jsonObject1.getString("date");
            String high=jsonObject1.getString("high");
            String low=jsonObject1.getString("low");
            String ymd=jsonObject1.getString("ymd");
            String week=jsonObject1.getString("week");
            String sunrise=jsonObject1.getString("sunrise");
            String sunset=jsonObject.getString("sunset");
            String aqi=jsonObject1.getString("aqi");
            String fx=jsonObject1.getString("fx");
            String fl=jsonObject1.getString("fl");
            String type=jsonObject1.getString("type");
            String notice=jsonObject1.getString("notice");

             Data=date+high+low+ymd+week+sunrise+sunset+aqi+fx+fl+type+notice;
            //创建message对象
            Message message=new Message();
           message.what=1;
            handler.sendMessage(message);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    //这个方法让异步消息代替了,因为查到网上的资料说是有延迟的,而程序运行速度太快,所以换成了handler,但是
    //发现还是没有用
//调用runOnUiThread方法来实现UI更新
    private void showResponse(final String response){
        //匿名内部类的方式实现run方法
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //在这里进行UI操作
                textView.setText(response);
            }
        });
    }
}


  • 写回答

2条回答 默认 最新

  • 键盘舞者113 博客专家认证 2020-04-18 20:12
    关注

    你用jsonformat插件,通过json结构生产实体类,然后 解析json数据时直接解析成实体类就好了

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?