我想解析JSON
对普通String
的回复,但当我尝试返回jsonString
时,我收到以下消息:
Incompatible types:
Required:
android.content.Loader
<java.lang.String>
Found:
java.lang.String
你能告诉我,我做错了什么吗?
package com.example.entropy.booklist;
import android.app.LoaderManager;
import android.content.Loader;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import static android.R.attr.data;
/**
* Created by entropy on 18/07/17.
*/
public class AllInOne extends AppCompatActivity implements LoaderManager.LoaderCallbacks<String> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_in_one);
getLoaderManager().initLoader(0, null, this);
}
String jsonStr ;
@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String line;
try {
URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null)
{return null;}
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) buffer.append(line);
if (buffer.length() == 0)
{return null;}
jsonStr = buffer.toString();
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) urlConnection.disconnect();
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("MainActivity", "Error closing stream", e);
}
}
}
return jsonStr;
}
答案 0 :(得分:1)
首先,您的错误消息绝对正确&#34;不兼容的类型:必需:android.content.Loader找到:java.lang.String&#34;因为您要返回String
,但方法需要Loader<String>
。
其次,你以错误的方式使用加载器。数据加载不应在onCreateLoader()
。请查看此链接以获取正确的信息,请在继续执行任何实现之前阅读本文档。
https://developer.android.com/guide/components/loaders.html
另外,请查看同一文档中的AsyncTaskLoader
。这将满足您从服务器加载数据的目的。
答案 1 :(得分:0)
您应该按照签名建议的方法onCreateLoader
返回一个Loader。因此,创建一个AsyncTaskLoader<String>
,在其中完成您的工作(它应该在后台完成,因为您通过网络获取数据)。
你应该这样做。
@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return new AsyncTaskLoader<String>(this) {
@Override
protected String loadInBackground() {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String line;
try {
URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null)
{return null;}
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) buffer.append(line);
if (buffer.length() == 0)
{return null;}
jsonStr = buffer.toString();
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) urlConnection.disconnect();
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("MainActivity", "Error closing stream", e);
}
}
}
return jsonStr;
}
}
}
答案 2 :(得分:0)
public class AllInOne extends AppCompatActivity implements LoaderManager.LoaderCallbacks<String> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_in_one);
getLoaderManager().initLoader(0, null, this);
BookAdapter adapter = new BookAdapter(this, books);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
public static String jsonStr;
@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return new GetResponse(this);
}
private static class GetResponse extends AsyncTaskLoader<String> {
public GetResponse(Context context) {
super(context);
}
@Override
public String loadInBackground() {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String line;
try {
URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2012-01-01&endtime=2012-12-01&minmagnitude=6");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) buffer.append(line);
if (buffer.length() == 0) {
return null;
}
jsonStr = buffer.toString();
} catch (IOException e) {
return null;
} finally {
if (urlConnection != null) urlConnection.disconnect();
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("MainActivity", "Error closing stream", e);
}
}
}
return jsonStr;
}
}