将String jsonString的所有坐标传递给swipe方法

时间:2014-01-22 00:41:59

标签: java android json uiautomator

如何在数组中创建每个数组,以便创建一个新的Point对象并将其添加到向量

public class SendText extends UiAutomatorTestCase {

    private String jsonString = "{\"A\": [[107,729],[108,728],[108,728],[108,727]," +
        "[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[108,727],[110,724]," +
        "[114,717],[125,701],[134,685],[145,663],[157,636],[169,607],[179,583],[191,558],[196,547]," +
        "[199,540],[201,534],[202,529],[203,528],[204,525],[204,524],[204,524],[204,524],[204,524]," +
        "[204,524],[204,525],[204,530],[205,538],[207,549],[209,566],[216,602],[223,629],[229,653]," +
        "[235,671],[238,684],[241,697],[242,703],[242,707],[243,710],[243,712],[243,713],[243,713]," +
        "[243,713],[243,713],[244,713],[244,713],[244,713],[244,713],[242,709],[237,703],[230,695]," +
        "[224,684],[220,679],[215,671],[212,665],[206,658],[202,655],[200,654],[196,651],[192,648]," +
        "[189,646],[184,642],[182,641],[180,639],[179,637],[177,635],[176,634],[174,633],[173,631]," +
        "[172,630],[171,629],[170,628],[170,628],[170,627],[169,627],[169,627],[169,627],[169,627]," +
        "[169,629],[169,629]]}" ;

    public void testSendText() throws UiObjectNotFoundException {   

        JSONObject jsonObj;
        try {
            jsonObj = new JSONObject(jsonString);
            jsonObj.getJSONArray("A");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        for(int k = 0; k < jsonString.length(); k++ ){
            Vector<Point> apoints = new Vector<Point>();
            apoints.add(new Point());

            getUiDevice().swipe((Point[])apoints.toArray(), 5);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我假设你定义了一个Point类。我们的想法是循环遍历数组A以获取每个子数组,并为每个子数组创建一个新的Point对象。

public void testSendText() throws JSONException {       
        JSONObject jsonObj = new JSONObject(jsonString);
        JSONArray  jsonArr = jsonObj.getJSONArray("A"); //get array A

        Vector<Point> apoints = new Vector<Point>();
        for(int k = 0; k < jsonArr.length(); k++ ){
            JSONArray arr = jsonArr.getJSONArray(k);
            apoints.add(new Point(arr.getInt(0), arr.getInt(1)));
        }
        getUiDevice().swipe(apoints.toArray(new Point[apoints.size()]), 5);
}

输出:

01-22 00:50:52.677: I/System.out(568): 
[Point [x=107, y=729], Point [x=108, y=728], Point [x=108, y=728], 
Point [x=108, y=727], Point [x=108, y=727], Point [x=108, y=727], 
Point [x=108, y=727], Point [x=108, y=727], Point [x=108, y=727], 
Point [x=108, y=727], Point [x=108, y=727], Point [x=108, y=727], 
Point [x=110, y=724], Point [x=114, y=717], Point [x=125, y=701], 
Point [x=134, y=685], Point [x=145, y=663], Point [x=157, y=636], 
Point [x=169, y=607], Point [x=179, y=583], Point [x=191, y=558], 
Point [x=196, y=547], Point [x=199, y=540], Point [x=201, y=534], 
Point [x=202, y=529], Point [x=203, y=528], Point [x=204, y=525], 
Point [x=204, y=524], Point [x=204, y=524], Point [x=204, y=524], 
Point [x=204, y=524], Point [x=204, y=524], Point [x=204, y=525], 
Point [x=204, y=530], Point [x=205, y=538], Point [x=207, y=549], 
Point [x=209, y=566], Point [x=216, y=602], Point [x=223, y=629], 
Point [x=229, y=653], Point [x=235, y=671], Point [x=238, y=684], 
Point [x=241, y=697], Point [x=242, y=703], Point [x=242, y=707], 
Point [x=243, y=710], Point [x=243, y=712], Point [x=243, y=713], 
Point [x=243, y=713], Point [x=243, y=713], Point [x=243, y=713], 
Point [x=244, y=713], Point [x=244, y=713], Point [x=244, y=713], 
Point [x=244, y=713], Point [x=242, y=709], Point [x=237, y=703], 
Point [x=230, y=695], Point [x=224, y=684], Point [x=220, y=679], 
Point [x=215, y=671], Point [x=212, y=665], Point [x=206, y=658], 
Point [x=202, y=655], Point [x=200, y=654], Point [x=196, y=651], 
Point [x=192, y=648], Point [x=189, y=646], Point [x=184, y=642], 
Point [x=182, y=641], Point [x=180, y=639], Point [x=179, y=637],
Point [x=177, y=635], Point [x=176, y=634], Point [x=174, y=633], 
Point [x=173, y=631], Point [x=172, y=630], Point [x=171, y=629], 
Point [x=170, y=628], Point [x=170, y=628], Point [x=170, y=627], 
Point [x=169, y=627], Point [x=169, y=627], Point [x=169, y=627], 
Point [x=169, y=627], Point [x=169, y=629], Point [x=169, y=629]]