我需要在我的类中使用共享首选项,但是当我使用getSharedReferences方法时,它会说“cannotresolve method'getSharedReferences(java.lang.string,int)”。代码如下:
public class FetchData extends AsyncTask<Void, Void, Void> {
String data = "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://api.jsonbin.io/b/5e42776dd18e4016617690ce/3");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
JSONArray JA = new JSONArray(data);
SharedPreferences sharedPreferences = getSharedPreferences("SHARED_PREFS", Context.MODE_PRIVATE); // THE ERROR
SharedPreferences.Editor editor = sharedPreferences.edit();
// for(int i =0 ;i < JA.length(); i++) {
// JSONObject JO = (JSONObject) JA.get(i);
// singleParsed =
// "id: " + JO.get("id") + "\n" +
// "img: " + JO.get("img") + "\n" +
// "w1: " + JO.get("w1") + "\n" +
// "w2: " + JO.get("w2") + "\n" +
// "w3: " + JO.get("w3") + "\n" +
// "w4: " + JO.get("w4") + "\n";
//
// dataParsed = dataParsed + singleParsed + "\n";
//
// }
2条答案
按热度按时间aurhwmvo1#
你应该这样做,
和
sqougxex2#
您可以使用github库来实现shareprefrence这里是link-easyprefs库
此库为您提供SharedReference函数。