java - 发送请求时未加载 JSON 数据
问题描述
我正在尝试构建一个 JSON 请求应用程序。它从这个网站“https://earthquake.usgs.gov/fdsnws/event/1/query”获取地震参数。我面临的问题是,当应用程序 Activity 启动时,只有进度条可见,但没有显示 JSON 数据,也没有显示 textview,只有进度条一直加载到最后。我试过但无法解决错误所在。请帮帮我,我刚在这里呆了几天。先感谢您
我的主要活动代码
package com.example.volleyearthquake;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RequestQueue mRequestQueue;
private ArrayList<Earthquake> mEarthquake;
private RecyclerView mRecycleView;
private EarthquakeAdapter adapter;
boolean isConnected;
/**
* Textview to be displayed when the listview have no data
*/
TextView emptyView;
/**
* Progressbar variable which is circular spinner (loading indicator)
*/
ProgressBar loadingIndicator;
private static final String USS_REQUEST_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Read_network_state(cm);
loadingIndicator = findViewById(R.id.loading_indicator);
emptyView = findViewById(R.id.empty_view);
mRecycleView = findViewById(R.id.recycle_list);
mRecycleView.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycleView.setLayoutManager(manager);
mEarthquake = new ArrayList<>();
adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
mRecycleView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mRequestQueue = Volley.newRequestQueue(this);
parseEarthquake();
}
private void parseEarthquake() {
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, USS_REQUEST_URL,
null, response -> {
try {
JSONArray feature = response.getJSONArray("features");
for (int index = 0; index < feature.length(); index++) {
JSONObject features = feature.getJSONObject(index);
JSONObject properties = features.getJSONObject("properties");
Double magnitude = properties.getDouble("mag");
String location = properties.getString("place");
long time = properties.getLong("time");
String url = properties.getString("url");
mEarthquake.add(new Earthquake(magnitude, location, time, url));
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
});
mRequestQueue.add(request);
}
public void Read_network_state(ConnectivityManager connectivityManager) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
isConnected = true;
Log.d("Ruhul", "CONNECTED TO INTERNET");
} else {
isConnected = false;
}
}
}
在清单文件中,我还添加了
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
解决方案
首先你必须解析Earthquakes,然后把它们放在recyclerView中,如果仍然有错误,尝试一步一步跟踪你的请求并检查locat以确保你正确解析json并且解析它时没有错误,提示:json解析时发生错误不会导致应用程序崩溃...希望我的回答对您有所帮助^_^
推荐阅读
- java - 赋予令牌高于另一个令牌的优先级
- r - 包装 ggplot2 文本以适应绘图宽度
- ios - 基于 NSFetchedResultsControllerDelegate 刷新 tableview
- ios - 表格视图中静态表格单元格的单元格内容对齐
- sql-server - 在 Xamarin Android 上使用 Web API 仅显示部分数据时如何修复错误
- laravel - Eloquent Laravel 按相关模型排序分页查询
- c# - 异步等待不适用于动态参数
- kubernetes - 如何将 .NET Core 发布到 Digital Ocean Kubernetes
- ios - 不能满足协议(与相关类型)的一致性
- python - 基于python GPU的批量点积