Java JSON数据不';发送请求时不加载
我正在尝试构建一个JSON请求应用程序。它从该网站获取地震参数“https://earthquake.usgs.gov/fdsnws/event/1/query". 我所面临的问题是,当应用程序活动开始时,进度条是可见的,但没有显示JSON数据,也没有显示textview,只有进度条一直加载到最后。我试过了,但无法解决错误所在。请帮帮我,我只是在这里呆了几天。先谢谢你 我的主要活动代码Java JSON数据不';发送请求时不加载,java,android,json,android-studio,android-volley,Java,Android,Json,Android Studio,Android Volley,我正在尝试构建一个JSON请求应用程序。它从该网站获取地震参数“https://earthquake.usgs.gov/fdsnws/event/1/query". 我所面临的问题是,当应用程序活动开始时,进度条是可见的,但没有显示JSON数据,也没有显示textview,只有进度条一直加载到最后。我试过了,但无法解决错误所在。请帮帮我,我只是在这里呆了几天。先谢谢你 我的主要活动代码 package com.example.volleyearthquake; import androidx.
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;
}
}
}
package com.example.volley地震;
导入androidx.appcompat.app.appcompat活动;
导入androidx.recyclerview.widget.LinearLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.ProgressBar;
导入android.widget.TextView;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonArrayRequest;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入org.json.JSONStringer;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
私有请求队列mRequestQueue;
私人ArrayList mEarthquake;
私人回收视图mRecycleView;
私人地震适配器;
布尔不连通;
/**
*listview没有数据时显示的Textview
*/
文本视图清空视图;
/**
*Progressbar变量,它是圆形微调器(加载指示器)
*/
进度条加载指示器;
私有静态最终字符串USS_请求_URL=”https://earthquake.usgs.gov/fdsnws/event/1/query";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ConnectivityManager cm=(ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_服务);
读取网络状态(cm);
加载指示器=findViewById(R.id.加载指示器);
emptyView=findViewById(R.id.empty\u视图);
mRecycleView=findviewbyd(R.id.recycle\u列表);
mRecycleView.setHasFixedSize(true);
LinearLayoutManager=新的LinearLayoutManager(此);
mRecycleView.setLayoutManager(管理器);
mEarthquake=newarraylist();
适配器=新的地震适配器(main activity.this,mEarthquake);
mRecycleView.setAdapter(适配器);
adapter.notifyDataSetChanged();
mRequestQueue=Volley.newRequestQueue(this);
地震();
}
私人有限公司(){
final JsonObjectRequest=新JsonObjectRequest(request.Method.GET,USS_request_URL,
空,响应->{
试一试{
JSONArray feature=response.getJSONArray(“features”);
对于(int index=0;index{
});
mRequestQueue.add(请求);
}
public void Read_network_state(ConnectivityManager ConnectivityManager){
NetworkInfo-activeNetwork=connectivityManager.getActiveNetworkInfo();
if(activeNetwork!=null&&activeNetwork.isConnectedOrConnecting()){
断开连接=正确;
Log.d(“Ruhul”,“连接到互联网”);
}否则{
断开连接=错误;
}
}
}
在清单文件中,我还添加了
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
请检查是否存在JSON异常。最有可能的是,这就是您解析错误的原因,如果不检查JSON响应,很难判断您是否正确解析了内容。放置parse地震();高于 mRequestQueue=Volley.newRequestQueue(this) 像这样
mRecycleView.setAdapter(adapter);
adapter.notifyDataSetChanged();
parseEarthquake();
mRequestQueue = Volley.newRequestQueue(this);
如果dosetn work共享您的错误,请。首先您必须解析地震,然后将其放入recyclerView中,如果您仍然有错误,请尝试一步一步地跟踪您的请求并检查locat,以确保您正确解析json,并且在解析时没有错误,
提示:当解析应用程序时发生错误时,json不会导致应用程序崩溃。。。我希望我的回答能在您如上所述更改代码后对您有所帮助。应用程序崩溃并出现错误
,原因是:java.lang.NullPointerException:尝试在com.example.volleySeventure.MainActivity.ParseSeventure(MainActivity.java:103)的空对象引用上调用虚拟方法“com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)”在com.example.volleySeventure.MainActivity.onCreate(MainActivity.java:69)
69行was-parseSeventure();103行为mRequestQueue.add(request);我是这样写的catch(JSONException e){Log.e(“QueryUtils”,“解析地震JSON结果时出现问题”,e);}
然后我对它进行日志跟踪,但当我在日志跟踪中搜索“QueryUtils”时,什么都没有显示。我想我犯了一些错误