Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Socket.IO获取和保存JSON数组_Java_Android_Json_Arraylist_Indexoutofboundsexception - Fatal编程技术网

Java 从Socket.IO获取和保存JSON数组

Java 从Socket.IO获取和保存JSON数组,java,android,json,arraylist,indexoutofboundsexception,Java,Android,Json,Arraylist,Indexoutofboundsexception,Im从我的服务器获取一组对象,如下所示: Log.i(TAG, "Destinos obtenidos: "+ destinosRuta); 原木价值- Destinos obtenidos[{“Latitud”:41.40404,“nombreDestino”:“巴塞罗那”,“IDR”:5,“IDD”:6,“Longitud”:2.168679},{“Latitud”:37.408424,“nombreDestino”:“塞维利亚”,“IDR”:5,“IDD”:7,“Longitud

Im从我的服务器获取一组对象,如下所示:

 Log.i(TAG, "Destinos obtenidos: "+ destinosRuta);   
原木价值-

Destinos obtenidos[{“Latitud”:41.40404,“nombreDestino”:“巴塞罗那”,“IDR”:5,“IDD”:6,“Longitud”:2.168679},{“Latitud”:37.408424,“nombreDestino”:“塞维利亚”,“IDR”:5,“IDD”:7,“Longitud”:-5.9681},{“Latitud”:38.92298,“nombreDestino”:“梅里达”,“IDR”:5,“IDD”:4,“Longitud”:-6.363121}

我想在SharedReferences上存储它,我使用了以下答案中的代码:

但我不知道是需要使用对象数组,还是需要转换为字符串数组,这是我的代码:

private Emitter.Listener onNuevaRuta = new Emitter.Listener() {

  @Override
  public void call(Object... args) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {


           JSONObject data = (JSONObject) args[0];
           String destinosRuta = "";


           try {
               destinosRuta = data.getString("destinosRuta");
            }catch (JSONException e) {
                 return;
            }

            //destinosRuta has the previous value
            ArrayList<String> listaDestinos = new ArrayList<String>(Arrays.asList(destinosRuta));

            setStringArrayPref(getApplicationContext(), "listaDestinos", listaDestinos);

             listaDestinos = getStringArrayPref(getApplicationContext(), "listaDestinos");

            String origen = listaDestinos.remove(0);
            String destino = listaDestinos.remove(0);

            setStringArrayPref(getApplicationContext(), "listaDestinos", listaDestinos);
    ...
private Emitter.Listener onNuevaRuta=new Emitter.Listener(){
@凌驾
公共无效调用(对象…参数){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
JSONObject数据=(JSONObject)参数[0];
字符串destinosRuta=“”;
试一试{
destinosRuta=data.getString(“destinosRuta”);
}捕获(JSONException e){
返回;
}
//destinosRuta具有以前的值
ArrayList listaDestinos=新的ArrayList(Arrays.asList(destinosRuta));
setStringarayPref(getApplicationContext(),“listaDestinos”,listaDestinos);
listaDestinos=getStringArrayPref(getApplicationContext(),“listaDestinos”);
字符串origen=listaDestinos.remove(0);
字符串destino=listaDestinos.remove(0);
setStringarayPref(getApplicationContext(),“listaDestinos”,listaDestinos);
...
和前面的链接一样,我使用了他的功能:

public static void setStringArrayPref(Context context, String key, ArrayList<String> values) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = prefs.edit();
    JSONArray a = new JSONArray();
    for (int i = 0; i < values.size(); i++) {
        a.put(values.get(i));
    }
    if (!values.isEmpty()) {
        editor.putString(key, a.toString());
    } else {
        editor.putString(key, null);
    }
    editor.commit();
}
public static ArrayList<String> getStringArrayPref(Context context, String key) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String json = prefs.getString(key, null);
    ArrayList<String> destinos = new ArrayList<String>();
    if (json != null) {
        try {
            JSONArray a = new JSONArray(json);
            for (int i = 0; i < a.length(); i++) {
                String destino = a.optString(i);
                destinos.add(destino);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return destinos;
}
public static void setStringarayPref(上下文上下文、字符串键、ArrayList值){
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(上下文);
SharedReferences.Editor=prefs.edit();
JSONArray a=新的JSONArray();
对于(int i=0;i
我尝试删除listaDestinos的第一个元素时出错:

字符串origen=listaDestinos.remove(0)


String destino=listaDestinos.remove(0);您不需要存储列表对象。如果您的服务器已经返回JSON,您可以直接存储该对象

JSONObject data = (JSONObject) args[0];
String response = String.valueOf(data);
//... Or parse out the data you need 
sharedPrefs.putString("response", response);
将字符串转换为JSON对象或其他任何形式,并可选地将其解析为实际的Java对象(您可以使用Jackson或Gson来帮助您)

无论如何,我不知道为什么您认为需要从列表中删除一个对象才能提取值;此外,看起来您只向列表中添加了一个值,而不是两个值。
Arrays.asList()
方法正在创建一个包含1字符串的列表。您无法从该列表中删除两个对象


因此,尝试使用一些断点和更多日志语句更好地调试代码

换句话说,您似乎在解析数据时遇到了问题,而不仅仅是使用SharedReference

比如说,

data.getString("destinosRuta");

这实际上是字符串还是数组?如果是数组,请使用JSONObject类的相应方法获取数组。

您不需要存储列表对象。如果您的服务器已经返回JSON,则可以直接存储它

JSONObject data = (JSONObject) args[0];
String response = String.valueOf(data);
//... Or parse out the data you need 
sharedPrefs.putString("response", response);
将字符串转换为JSON对象或其他任何形式,并可选地将其解析为实际的Java对象(您可以使用Jackson或Gson来帮助您)

无论如何,我不知道为什么您认为需要从列表中删除一个对象才能提取值;此外,看起来您只向列表中添加了一个值,而不是两个值。
Arrays.asList()
方法正在创建一个包含1字符串的列表。您无法从该列表中删除两个对象


因此,尝试使用一些断点和更多日志语句更好地调试代码

换句话说,您似乎在解析数据时遇到了问题,而不仅仅是使用SharedReference

比如说,

data.getString("destinosRuta");

这实际上是字符串还是数组?如果是数组,请使用JSONObject类的相应方法获取数组。

将json保存到首选项,如下所示

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
prefsEditor.putString("MyObject", destinosRuta);
prefsEditor.commit();
并在使用和转换为ArrayList或List时获取它,如下所示

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = appSharedPrefs.getString("MyObject", "");
Type type = new TypeToken<List<Object>>(){}.getType();
List<Object> objects= gson.fromJson(json, type);
SharedReferences-AppSharedRefers=PreferenceManager.GetDefaultSharedReferences(this.getApplicationContext());
Gson Gson=新的Gson();
String json=appSharedPrefs.getString(“MyObject”,即“”);
Type Type=new-TypeToken(){}.getType();
List objects=gson.fromJson(json,类型);

像这样将json保存到首选项

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
prefsEditor.putString("MyObject", destinosRuta);
prefsEditor.commit();
并在使用和转换为ArrayList或List时获取它,如下所示

SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = appSharedPrefs.getString("MyObject", "");
Type type = new TypeToken<List<Object>>(){}.getType();
List<Object> objects= gson.fromJson(json, type);
SharedReferences-AppSharedRefers=PreferenceManager.GetDefaultSharedReferences(this.getApplicationContext());
Gson Gson=新的Gson();
String json=appSharedPrefs.getString(“MyObject”,即“”);
Type Type=new-TypeToken(){}.getType();
List objects=gson.fromJson(json,类型);

因为我需要保存数组以使其持久,正如我搜索并说的那样,这是最好的选择更好的选择是使用Realm数据库,它本机存储JSON。另外,您使用的是Socket.io,它会继续向您发送数据。SharedReference只能在同一个键上存储一个对象