json应用程序仅从android studio执行
你好吗 在过去的几天里,我构建了我的新jSon应用程序。 它从这个网站获取了earthquak上的params 而且效果很好。但只有当我在谷歌工作室执行时。 如果我关闭应用程序并在手机上手动打开它,我根本看不到任何数据 我的意思是应用程序正在打开,但没有json数据。只有空白屏幕,没有文本视图,也没有列表视图 我在主要活动中使用了以下内容json应用程序仅从android studio执行,android,json,android-volley,Android,Json,Android Volley,你好吗 在过去的几天里,我构建了我的新jSon应用程序。 它从这个网站获取了earthquak上的params 而且效果很好。但只有当我在谷歌工作室执行时。 如果我关闭应用程序并在手机上手动打开它,我根本看不到任何数据 我的意思是应用程序正在打开,但没有json数据。只有空白屏幕,没有文本视图,也没有列表视图 我在主要活动中使用了以下内容 package com.example.erang.jsonsample; import android.os.Bundle; import androi
package com.example.erang.jsonsample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.android.volley.Cache;
import com.android.volley.Network;
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.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Earthquake> earthquakes = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createJsonRequest();
// Create an {@link EarthQukeAdapter}, whose data source is a list of {@link earthquake}s. The
// adapter knows how to create list items for each item in the list.
EarthQuakeAdapter adapter = new EarthQuakeAdapter(this, earthquakes);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// earthquake_activity.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link earthquake} in the list.
listView.setAdapter(adapter);
}
@Override
protected void onStop() {
super.onStop();
}
public void createJsonRequest() {
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
RequestQueue mRequestQueue = new RequestQueue(cache, network);
// Start the queue
mRequestQueue.start();
String url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson";
// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Passing the response
parseJsonRequest(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);
}
public void parseJsonRequest(String response) {
if (response != null) {
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
JSONObject reader = new JSONObject(response);
JSONArray features = reader.getJSONArray("features");
for(int index=0;index<features.length();index++){
JSONObject properties = reader.getJSONArray("features").getJSONObject(index).getJSONObject("properties");
Double magnitude = properties.getDouble("mag");
String place = properties.getString("place");
String url = properties.getString("url");
//This split between offset location and primary location
String[] separated = place.split(",");
//If there is missing field jump to the next item
if(separated.length < 2){
continue;
}
//This contain the offsetLocation
String offsetLocation = separated[0];
//This contain the primaryLocation
separated[1] = separated[1].trim();
String primaryLocation = separated[1];
//Passing date twice one for the date and one for the time.
String updateDate = properties.getString("time");
java.util.Date date=new java.util.Date(Long.valueOf(updateDate) );
//The second date passing the time
earthquakes.add(new Earthquake(magnitude, offsetLocation, primaryLocation, date, date, url));
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
}
}
}
你知道为什么我的应用程序只有在android Studio上运行时才会运行良好吗
在清单文件中,我还添加了
<uses-permission android:name="android.permission.INTERNET" />
非常感谢
Eran公共类MainActivity扩展了AppCompatActivity{
ArrayList地震=新ArrayList();
地震适配器;
列表视图列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createJsonRequest();
listView=(listView)findViewById(R.id.list);
}
@凌驾
受保护的void onStop(){
super.onStop();
}
public void createJsonRequest(){
//实例化缓存
Cache Cache=new DiskBasedCache(getCacheDir(),1024*1024);//1MB上限
//设置网络以使用HttpURLConnection作为HTTP客户端。
网络=新的基本网络(new HurlStack());
//使用缓存和网络实例化RequestQueue。
RequestQueue MRRequestQueue=新的RequestQueue(缓存,网络);
//启动队列
mRequestQueue.start();
字符串url=”https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson";
//制定请求并处理响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//传递响应
parseJsonRequest(响应);
适配器=新的地震适配器(main activity.this,地震);
setAdapter(适配器);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//处理错误
}
});
//将请求添加到RequestQueue。
mRequestQueue.add(stringRequest);
}
公共void parseJsonRequest(字符串响应){
if(响应!=null){
//捕获异常以便应用程序不会崩溃,并将错误消息打印到日志中。
试一试{
JSONObject reader=新的JSONObject(响应);
JSONArray features=reader.getJSONArray(“features”);
对于(int index=0;index1),我能够解决这个问题。
首先,我在看另一个使用Async的实现,我看到有一个onPostExecute方法在PostExecute上运行。
当我使用截击时,我寻找类似的东西。
我知道我需要使用OnResponse方法。
所以我换了3行代码
@Override
public void onResponse(String response) {
//Passing the response
parseJsonRequest(response);
EarthQuakeAdapter adapter = new EarthQuakeAdapter(MainActivity.this, earthquakes);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
},
在OnResponse中,因此只有在填充数组后,我才会创建适配器并将其添加到列表视图中。为什么它会为null。?首先,我没有得到任何异常。其次,我在这一行填充数组。添加(新地震(震级、偏移位置、原始位置、日期、日期、url));为什么它在Studio中执行时效果很好,而在手动执行时则会有所不同?我已经编辑了您的问题,请尝试。您的示例中的一个问题是listView.setAdapter(adapter);它在onCreate中定义,但在OnResponse中没有识别。但是我缺少创建数组的那一行。如果您完全删除它,只需做一件事,在parseJsonRequest(response)之后在代码中添加这一行适配器。notifyDataSetChanged();没有帮助…但是感谢您的努力Mohammad.,:-)
public class MainActivity extends AppCompatActivity {
ArrayList<Earthquake> earthquakes = new ArrayList<>();
EarthQuakeAdapter adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createJsonRequest();
listView = (ListView) findViewById(R.id.list);
}
@Override
protected void onStop() {
super.onStop();
}
public void createJsonRequest() {
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
// Instantiate the RequestQueue with the cache and network.
RequestQueue mRequestQueue = new RequestQueue(cache, network);
// Start the queue
mRequestQueue.start();
String url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson";
// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Passing the response
parseJsonRequest(response);
adapter = new EarthQuakeAdapter(MainActivity.this, earthquakes);
listView.setAdapter(adapter);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);
}
public void parseJsonRequest(String response) {
if (response != null) {
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
JSONObject reader = new JSONObject(response);
JSONArray features = reader.getJSONArray("features");
for(int index=0;index<features.length();index++){
JSONObject properties = reader.getJSONArray("features").getJSONObject(index).getJSONObject("properties");
Double magnitude = properties.getDouble("mag");
String place = properties.getString("place");
String url = properties.getString("url");
//This split between offset location and primary location
String[] separated = place.split(",");
//If there is missing field jump to the next item
if(separated.length < 2){
continue;
}
//This contain the offsetLocation
String offsetLocation = separated[0];
//This contain the primaryLocation
separated[1] = separated[1].trim();
String primaryLocation = separated[1];
//Passing date twice one for the date and one for the time.
String updateDate = properties.getString("time");
java.util.Date date=new java.util.Date(Long.valueOf(updateDate) );
//The second date passing the time
earthquakes.add(new Earthquake(magnitude, offsetLocation, primaryLocation, date, date, url));
}
} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
}
}
}
@Override
public void onResponse(String response) {
//Passing the response
parseJsonRequest(response);
EarthQuakeAdapter adapter = new EarthQuakeAdapter(MainActivity.this, earthquakes);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
},