Question

I am calling a webservice that would do a post to one of my client database. The method is expecting a sessoionId, and a jagged array. It also return a jagged array. I am having an issue passing the jagged array to the JSONStringer and StringEntity. Below is a simple of my code inside the 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)

// I am not sure how to format the jaggedArray in here.

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

//Converting the below parameter to a string does not help either.

                     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();

//when I look at the resultFromPost, it failed with the message String did not format properly.

                        resultFromPost= new String(buffer1);

            }

Any help or suggestion would be greatly appreciated.

Was it helpful?

Solution

The parameters must be built using JSONArray instead of

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"});

Instead use this:

JSONArray jArray= new JSONArray();
jArray.put(0, "example");
jArray.put(1, "Text");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top