将Jagged数组传递给JSONoBJECT和StringEntity

时间:2013-04-30 12:34:57

标签: java android jagged-arrays

我正在调用一个Web服务,它会对我的一个客户端数据库发帖。该方法期待sessoionId和锯齿状数组。它还返回一个锯齿状的数组。我有一个问题将锯齿状数组传递给JSONStringer和StringEntity。下面是doinbackground中我的简单代码:

if(sessionId != "")
            {
                  URL = "http://10.0.2.2:88/Student/Grade";
                  requestPost = new HttpPost(URL);
                  requestPost.setHeader("Accept", "application/json");
                  requestPost.setHeader("Content-type", "application/json");

                  List<String[]> parameters = new ArrayList<String[]>();
                  parameters.add(new String[] {"StudentID","SSN"});
                  parameters.add(new String[] {"StudentLastName", "LastName"});
                  parameters.add(new String[] {"StudentGrade","Grade"});

                  JSONStringer VistAConnect = new JSONStringer()
                  .object()
                  .key("sessionId").value(sessionId)

//我不知道如何在这里格式化jaggedArray。

                  .key("JaggedArrayParameters").value(parameters)
                  .endObject();

//将以下参数转换为字符串也无济于事。

                     StringEntity entity = new StringEntity(VistAConnect.toString());
                      requestPost.setEntity(entity);
                      httpClient = new DefaultHttpClient();
                      HttpResponse response1 = httpClient.execute(requestPost); 
                      HttpEntity responseEntity1 =  response1.getEntity(); 

                      char[] buffer1 = new char[(int)responseEntity1.getContentLength()];
                        InputStream stream1 =responseEntity1.getContent();
                        InputStreamReader reader1 = new InputStreamReader(stream1);
                        reader1.read(buffer1);
                        stream1.close();

//当我查看resultFromPost时,它失败了,消息字符串没有正确格式化。

                        resultFromPost= new String(buffer1);

            }

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

必须使用JSONArray而不是

构建参数
List<String> or String[][] = {new [] String{"example","text"}
};
List<String[]> parameters = new ArrayList<String[]>();
parameters.add(new String[] {"StudentID","SSN"});
parameters.add(new String[] {"StudentLastName", "LastName"});
parameters.add(new String[] {"StudentGrade","Grade"});

而是使用它:

JSONArray jArray= new JSONArray();
jArray.put(0, "example");
jArray.put(1, "Text");