Эх сурвалжийг харах

RA Image Android

启动服务时加载保存的列表。
Ray Zhang 8 жил өмнө
parent
commit
a195202743

+ 1 - 1
RA Image/app/src/main/java/com/usai/redant/raimage/UploadList/UploadListActivity.java

@@ -128,7 +128,7 @@ public class UploadListActivity extends AppCompatActivity {
         @Override
         public void updateList()
         {
-
+            
         }
     };
 

+ 8 - 1
RA Image/app/src/main/java/com/usai/util/Network.java

@@ -1076,9 +1076,11 @@ public class Network
 		   String LINE_END = "\r\n";
 		String BOUNDARY = UUID.randomUUID().toString(); //边界标识 随机生成 String PREFIX = "--" , LINE_END = "\r\n";
 		String CONTENT_TYPE = "multipart/form-data"; //内容类型
+
+		HttpURLConnection conn = null;
 		try {
 			URL url = new URL(host);
-			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+			conn = (HttpURLConnection) url.openConnection();
 			conn.setReadTimeout(SO_TIMEOUT);
 			conn.setConnectTimeout(SO_TIMEOUT);
 			conn.setRequestMethod("POST"); //请求方式
@@ -1184,6 +1186,11 @@ public class Network
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
+		finally {
+			if(conn!=null){
+				conn.disconnect();
+			}
+		}
 
 		return null;
 	}

+ 38 - 5
RA Image/app/src/main/java/com/usai/util/RAUploadManager.java

@@ -2,6 +2,7 @@ package com.usai.util;
 
 import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.util.Base64;
 import android.util.Log;
 
 import com.usai.redant.raimage.RedAntApplication;
@@ -9,8 +10,10 @@ import com.usai.redant.raimage.RedAntApplication;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
@@ -214,16 +217,23 @@ public class RAUploadManager {
                     removefromlist=true;
                 else if (status==TaskStatus.TaskStatusError&&removeError)
                     removefromlist=true;
-                if(removefromlist)
+
+                synchronized (this)
                 {
-                    synchronized (this)
+
+                    if(removefromlist)
                     {
-                        arr_queue.remove(taskinfo);
-                        uiUpdateListener.updateList();
+                            arr_queue.remove(taskinfo);
+                        if(uiUpdateListener!=null)
+                            uiUpdateListener.updateList();
+
 
                     }
+                    int a=0;
+                    saveTasks();
                 }
 
+
             }
         });
 
@@ -263,9 +273,15 @@ public class RAUploadManager {
     };
     public void startTask(Bundle task)
     {
+        synchronized (this)
+        {
 
+            saveTasks();
+        }
         operation_queue.addOperation(task);
 
+
+
     };
     public void removeTask(Bundle task)
     {
@@ -328,14 +344,31 @@ public class RAUploadManager {
         SharedPreferences.Editor editor = pref.edit();
 
         try {
+
+            ArrayList<String> wrap_arr = new ArrayList<String>();
+            for( Bundle b:arr_queue)
+            {
+                wrap_arr.add(RAUtil.Bundle2Json(b).toString());
+            }
+
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(baos);
-            oos.writeObject(arr_queue);
+            oos.writeObject(wrap_arr);
 
             String personBase64 = android.util.Base64.encodeToString(baos.toByteArray(), android.util.Base64.DEFAULT);//new String(Base64.encodeBase64(baos.toByteArray()));
+
+
+            byte[] decode=Base64.decode(personBase64,Base64.DEFAULT);
+            ByteArrayInputStream bais= new ByteArrayInputStream(decode);
+
+            ObjectInputStream ois = new ObjectInputStream(bais);
+            ArrayList<String> arr_load=(ArrayList<String>)ois.readObject();
+            int a = 0;
+
         }
         catch (Exception e)
         {
+            e.printStackTrace();
 
         }
 

+ 123 - 0
RA Image/app/src/main/java/com/usai/util/RAUtil.java

@@ -3,6 +3,17 @@ package com.usai.util;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.os.Bundle;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.lang.reflect.Array;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * Created by ray on 27/06/2017.
@@ -42,4 +53,116 @@ public class RAUtil {
         }
 
     }
+
+    public static JSONObject Bundle2Json(Bundle bundle)
+    {
+        JSONObject json = new JSONObject();
+        Set<String> keys = bundle.keySet();
+        for (String key : keys) {
+            try {
+                 json.put(key, wrap(bundle.get(key))); //see edit below
+//                json.put(key, JSONObject.wrap(bundle.get(key)));
+            } catch(JSONException e) {
+                //Handle exception here
+            }
+        }
+        return json;
+    }
+
+    private static Object wrap(Object o) {
+        if (o == null) {
+            return JSONObject.NULL;
+        }
+        if (o instanceof JSONArray || o instanceof JSONObject) {
+            return o;
+        }
+        if (o.equals(JSONObject.NULL)) {
+            return o;
+        }
+        try {
+            if (o instanceof Collection) {
+                return new JSONArray((Collection) o);
+            } else if (o.getClass().isArray()) {
+                return toJSONArray(o);
+            }
+            if (o instanceof Map) {
+                return new JSONObject((Map) o);
+            }
+            if (o instanceof Boolean ||
+                    o instanceof Byte ||
+                    o instanceof Character ||
+                    o instanceof Double ||
+                    o instanceof Float ||
+                    o instanceof Integer ||
+                    o instanceof Long ||
+                    o instanceof Short ||
+                    o instanceof String) {
+                return o;
+            }
+            if (o.getClass().getPackage().getName().startsWith("java.")) {
+                return o.toString();
+            }
+        } catch (Exception ignored) {
+        }
+        return null;
+    }
+
+    private static JSONArray toJSONArray(Object array) throws JSONException {
+        JSONArray result = new JSONArray();
+        if (!array.getClass().isArray()) {
+            throw new JSONException("Not a primitive array: " + array.getClass());
+        }
+        final int length = Array.getLength(array);
+        for (int i = 0; i < length; ++i) {
+            result.put(wrap(Array.get(array, i)));
+        }
+        return result;
+    }
+    public static Bundle Json2Bundle(JSONObject s) {
+        Bundle bundle = new Bundle();
+
+        for (Iterator<String> it = s.keys(); it.hasNext(); ) {
+            String key = it.next();
+            JSONArray arr = s.optJSONArray(key);
+            Double num = s.optDouble(key);
+            String str = s.optString(key);
+
+            if (arr != null && arr.length() <= 0)
+                bundle.putStringArray(key, new String[]{});
+
+            else if (arr != null && !Double.isNaN(arr.optDouble(0))) {
+                double[] newarr = new double[arr.length()];
+                for (int i=0; i<arr.length(); i++)
+                    newarr[i] = arr.optDouble(i);
+                bundle.putDoubleArray(key, newarr);
+            }
+
+            else if (arr != null && arr.optString(0) != null) {
+                String[] newarr = new String[arr.length()];
+                for (int i=0; i<arr.length(); i++)
+                    newarr[i] = arr.optString(i);
+                bundle.putStringArray(key, newarr);
+            }
+
+            else if (!num.isNaN())
+                bundle.putDouble(key, num);
+
+            else if (str != null)
+                bundle.putString(key, str);
+
+            else
+                System.err.println("unable to transform json to bundle " + key);
+        }
+        return bundle;
+    }
+//    public static Bundle Json2Bundle(JSONObject jsonObject) throws JSONException {
+//        Bundle bundle = new Bundle();
+//        Iterator iter = jsonObject.keys();
+//        while(iter.hasNext()){
+//            String key = (String)iter.next();
+//            String value = jsonObject.getString(key);
+//            bundle.putString(key,value);
+//        }
+//        return bundle;
+//    }
 }