英文:
Can't get data in JSON format
问题
以下是翻译好的部分:
"I want to get data in JSON format for my app, I tried different ways to solve this problem, but I couldn't find the answer at this error. I hope, you guys can help me, if you need more details, just tell me."
这是我收到的错误消息:
E/MainActivity: 解析地震JSON结果时出现问题
org.json.JSONException: 输入结束于字符 0 处
at org.json.JSONTokener.syntaxError(JSONTokener.java:460)
at org.json.JSONTokener.nextValue(JSONTokener.java:101)
at org.json.JSONObject.<init>(JSONObject.java:164)
at org.json.JSONObject.<init>(JSONObject.java:181)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.extractFeatureFromJson(MainActivity.java:207)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.doInBackground(MainActivity.java:121)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.doInBackground(MainActivity.java:104)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
从JSON格式中提取数据的方法:
private Event extractFeatureFromJson(String earthquakeJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(earthquakeJSON);
JSONArray featureArray = baseJsonResponse.getJSONArray("features");
// 如果在features数组中有结果
if (featureArray.length() > 0) {
// 提取出第一个feature(这是一个地震事件)
JSONObject firstFeature = featureArray.getJSONObject(0);
JSONObject properties = firstFeature.getJSONObject("properties");
// 提取标题、时间和海啸警报值
String title = properties.getString("title");
long time = properties.getLong("time");
int tsunamiAlert = properties.getInt("tsunami");
// 创建一个新的Event对象
return new Event(title, time, tsunamiAlert);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "解析地震JSON结果时出现问题", e);
}
return null;
}
将数据放入Event对象的方法:
@Override
protected Event doInBackground(URL... urls) {
// 创建URL对象
URL url = createUrl(USGS_REQUEST_URL);
// 执行HTTP请求到URL,并接收JSON响应
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO 处理IOException
e.printStackTrace();
}
// 从JSON响应中提取相关字段并创建一个Event对象
Event earthquake = extractFeatureFromJson(jsonResponse);
// 将Event对象作为TsunamiAsyncTask的结果返回
return earthquake;
}
英文:
I want to get data in JSON format for my app, I tried different ways to solve this problem, but I couldn't find the answer at this error. I hope, you guys can help me, if you need more details, just tell me.
This is the Error I get:
E/MainActivity: Problem parsing the earthquake JSON results
org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:460)
at org.json.JSONTokener.nextValue(JSONTokener.java:101)
at org.json.JSONObject.<init>(JSONObject.java:164)
at org.json.JSONObject.<init>(JSONObject.java:181)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.extractFeatureFromJson(MainActivity.java:207)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.doInBackground(MainActivity.java:121)
at com.example.android.soonami.MainActivity$TsunamiAsyncTask.doInBackground(MainActivity.java:104)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Method that extracts data in json format:
private Event extractFeatureFromJson(String earthquakeJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(earthquakeJSON);
JSONArray featureArray = baseJsonResponse.getJSONArray("features");
// If there are results in the features array
if (featureArray.length() > 0) {
// Extract out the first feature (which is an earthquake)
JSONObject firstFeature = featureArray.getJSONObject(0);
JSONObject properties = firstFeature.getJSONObject("properties");
// Extract out the title, time, and tsunami values
String title = properties.getString("title");
long time = properties.getLong("time");
int tsunamiAlert = properties.getInt("tsunami");
// Create a new {@link Event} object
return new Event(title, time, tsunamiAlert);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
}
return null;
}
Method that puts data in Event object:
@Override
protected Event doInBackground(URL... urls) {
// Create URL object
URL url = createUrl(USGS_REQUEST_URL);
// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
e.printStackTrace();
}
// Extract relevant fields from the JSON response and create an {@link Event} object
Event earthquake = extractFeatureFromJson(jsonResponse);
// Return the {@link Event} object as the result fo the {@link TsunamiAsyncTask}
return earthquake;
}
答案1
得分: 0
我猜你从 jsonResponse = makeHttpRequest(url)
收到了空字符串。你有检查过吗?
英文:
I guess that you receive empty string from jsonResponse = makeHttpRequest(url)
.
Have you checked that?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论