问题描述:写了一个解析网络上天气预报的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);
}
});
}
}