2014-02-07 97 views
-1
package automate; 

    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.IOException; 
    import java.util.HashMap; 
    import java.util.Map; 
    import java.util.Vector; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import android.graphics.Point; 
    import com.android.uiautomator.core.UiDevice; 
    import com.android.uiautomator.testrunner.UiAutomatorTestCase; 

    public class AlphabetGestures extends UiAutomatorTestCase { 
     private static Map<Character,Vector<Point>> map = new HashMap<Character,Vector<Point>>() ; 
     private static JSONObject jsonObj; 
     static{ 
       String text; 
       try { 
         File file = new File("/data/misc/alphaNums.json"); 
         FileInputStream fis = new FileInputStream(file); 
         byte[] data = new byte[(int)file.length()]; 
         fis.read(data); 
         fis.close(); 
         text = new String(data, "UTF-8"); 
         jsonObj = new JSONObject(text); 
        } catch(JSONException e){ 
        e.printStackTrace(); 
        } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        } 

/*我不能夠實現以下算法: - 我想創建一個地圖中的所有字符< - 已經在那裏在靜態區塊 中,打開alphaNum.json文件< - 已經存在於靜態區塊中,但未進行錯誤處理。 解析它作爲一個JSON對象< - 已經存在於靜態集團,我有大量的冗餘代碼。該組信件進來的JSON文件,我想分析什麼是有

for every letter in the json object { 
    create a vector<Point> 
    for every Point in the letter { 
    add the Point to the vector 
    } 
    add the vector to the map 
} 

*/

 Vector<Point> liA = setupA(); 
     Vector<Point> liB = setupB(); 
     Vector<Point> liC = setupC(); 

     map.put('A',liA) ; 
     map.put('B',liB) ; 
     map.put('C',liC) ; 
    } 


    private static Vector<Point> setupA(){ 
     try { 
     JSONArray jsonArr = jsonObj.getJSONArray("A"); 
     System.out.println("IS here"); 
     //get array A 
     if(jsonArr!=null){ 
     Vector<Point> apoints = new Vector<Point>(); 
     System.out.println("IS here"); 
     for(int k = 0; k < jsonArr.length(); k++){ 
     JSONArray arr = jsonArr.getJSONArray(k); 
     apoints.add(new Point(arr.getInt(0), arr.getInt(1))); 
     System.out.println(apoints); 
     }return apoints ; 
     }else{ 
     System.out.println("A is null"); 
     } 
     } catch (JSONException e){ 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     return null; 
    } 

    private static Vector<Point> setupB(){ 
     try { 
     JSONArray jsonArr = jsonObj.getJSONArray("B"); 
     System.out.println("IS here"); 
     //get array B 
     if(jsonArr!=null){ 
     Vector<Point> apoints = new Vector<Point>(); 
     System.out.println("LUMIA IS here"); 
     for(int k = 0; k < jsonArr.length(); k++){ 
     JSONArray arr = jsonArr.getJSONArray(k); 
     apoints.add(new Point(arr.getInt(0), arr.getInt(1))); 
     System.out.println(apoints); 
     }return apoints ; 
     }else{ 
     System.out.println("B is null"); 
     } 
     } catch (JSONException e){ 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     return null; 
    } 

    private static Vector<Point> setupC(){ 
     try { 
     JSONArray jsonArr = jsonObj.getJSONArray("C"); 
     System.out.println("IS here"); 
     //get array C 
     if(jsonArr!=null){ 
     Vector<Point> apoints = new Vector<Point>(); 
     System.out.println(" IS here"); 
     for(int k = 0; k < jsonArr.length(); k++){ 
     JSONArray arr = jsonArr.getJSONArray(k); 
     apoints.add(new Point(arr.getInt(0), arr.getInt(1))); 
     System.out.println(apoints); 
     }return apoints ; 
     }else{ 
     System.out.println("C is null"); 
     } 
     } catch (JSONException e){ 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     return null; 
    } 


    public static void scribble(char letter, UiDevice uiDevice){ 
     System.out.println("here"); 
     Vector<Point> points = map.get(letter) ; 
     System.out.println("------------------"); 
     System.out.println(points); 
     if(points!=null){ 
     uiDevice.swipe(points.toArray(new Point[points.size()]), 5); 
     } 
     } 
    } 
+0

你可以添加一個樣本JSON文件,好嗎?我很難想象你想要做什麼。 –

+0

@ KenY-N以下是json文件的內容.... { 「A」:[[150,450],[150,150],[250,150],[250,450],[250,275],[150,275]], 「C」:[[232,552],[232,551],[[140,543],[ [231551],[230550],[223549],[217547],[210546],[202545],[187544],[175544],[162544],[147547],[122556],[114562],[111566 ],[103576],[95589],[89606],[86654],[93670],[105686],[126705],[144713],[165717],[189718],[221711],[234705], [243,700]] } – user3281879

回答

0

什麼

private static Vector<Point> setupLetter(String letter){ 
    try { 
    JSONArray jsonArr = jsonObj.getJSONArray(letter); 
    System.out.println("IS here"); 
    //get array corresponding to letter 
    if(jsonArr!=null){ 
    Vector<Point> letterPoints = new Vector<Point>(); 
    System.out.println("IS here"); 
    for(int k = 0; k < jsonArr.length(); k++){ 
    JSONArray arr = jsonArr.getJSONArray(k); 
    letterPoints.add(new Point(arr.getInt(0), arr.getInt(1))); 
    System.out.println(letterPoints); 
    }return letterPoints ; 
    }else{ 
    System.out.println("%s is null".format(letter)); 
    } 
    } catch (JSONException e){ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    return null; 
} 
+0

它給我NULL – user3281879

+0

它在哪裏給你NULL?你是否傳遞了一封適當的信? – Catherine

+0

它不需要任何字母,只是簡單地返回空....是的,我通過「C」! – user3281879

相關問題