如何使用AsyncTask从SharePreference进行JSON解析URL,无法从SharePreference执行URL

时间:2019-10-21 11:23:18

标签: java android json parsing

如何使用AsyncTask对来自SharePreference的URL进行JSON解析,无法从SharePreference执行URL。但是当URL iset Manual From Activity而不是getString SharePreference结果是可以的。

保存SharePreference

SharedPreferences.Editor editor;
SharedPreferences pref = getSharedPreferences("Options", Context.MODE_PRIVATE);
editor = pref.edit();

String a = String.valueOf(obj.texta);
                    byte[] idsek64 = Base64.encode(a.getBytes(), Base64.DEFAULT);
                    String idsek64siap = new String(idsek64);
                    String idsekrot13 = rot13(idsek64siap);
                    String UrlSek = "https://128.60.12.111/contacts/"+idsekrot13+"/id?_s=ARLlBQWRAR";
                    editor.putString("proc",UrlSek);
                    editor.commit();

从SharePreference和Proses中获取数据 我从SharePreference,SharePreference字符串存储中获取的字符串是合适的,当我放置Toast时,我对其进行了证明。

SharedPreferences pref = getSharedPreferences("Options", Context.MODE_PRIVATE);
        String urls = pref.getString("proc","");
if (HttpHandler.isNetworkAvailable(MainActivity.this)) {
            new MyTask().execute(urls);

        } else {

Toast.makeText(MainActivity.this, "NO INTERNET!", Toast.LENGTH_LONG).show();
        }

//PROSES MYTASK EXECUTE URL
    class MyTask extends AsyncTask<String, Void, String> {
        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected String doInBackground(String... params) {
            return HttpHandler.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }
            if (null == result || result.contains("[]")) {
                customtoast2.show();
            } else {
                try {
                    JSONArray jsonArray = new JSONArray(result);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONArray subArray = jsonArray.getJSONArray(i);

但是当即时设置网址手册看起来像:

String urls ="https://128.60.12.111/contacts/curent_job/id?_s=ARLlBQWRAR"
if (HttpHandler.isNetworkAvailable(MainActivity.this)) {
            new MyTask().execute(urls);

        } else {
    ....
}

出来的结果将是我想要的方式。

如果有人帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:0)

我认为您的存储部分有问题。

 public class SharedPrefsActivity extends Activity {

 public static final String PREFS_NAME = "MyApp_Settings";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

  // Writing data to SharedPreferences
  Editor editor = settings.edit();
  editor.putString("key", "some value");
  editor.commit();

  // Reading from SharedPreferences
  String value = settings.getString("key", "");
  Log.d(TAG, value);
 }
}

您是否正在使用此“选项”进行存储?还是您正在使用带有两个不同标签的两个实例? 。最好使用相同的SharedPreferences实例或相同的标签 确保您的存储还可以,祝您好运

答案 1 :(得分:0)

使用PrefsUtils类而不是像这样保存它。

/**
* A pack of helpful getter and setter methods for reading/writing to {@link SharedPreferences}.
*/
final public class SharedPrefsUtils {
private SharedPrefsUtils() {}

/**
 * Helper method to retrieve a String value from {@link SharedPreferences}.
 *
 * @param context a {@link Context} object.
 * @param key
 * @return The value from shared preferences, or null if the value could not be read.
 */
public static String getStringPreference(Context context, String key) {
    String value = null;
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (preferences != null) {
        value = preferences.getString(key, null);
    }
    return value;
}

/**
 * Helper method to write a String value to {@link SharedPreferences}.
 *
 * @param context a {@link Context} object.
 * @param key
 * @param value
 * @return true if the new value was successfully written to persistent storage.
 */
public static boolean setStringPreference(Context context, String key, String value) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (preferences != null && !TextUtils.isEmpty(key)) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }
    return false;
 }
}

在活动中添加以下行以设置为首选

SharedPrefsUtils.setStringPreference(this,"your_key","your_value");

使用偏好值

SharedPrefsUtils.getStringPreference(this,"your_key");