I would like to update a different thread that I made with some code that I have written that is not working. I am trying to parse my information, which after sending a post request, looks like this [{"fromUser":"Andrew"},{"fromUser":"Jimmy"}]
I would then like to take those users, and add them to a list view. Here is my code for sending the HTTPpost and then also my code for trying to parse and put it into the adapter.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(htmlUrl);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("Username", "Brock"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
try {
JSONObject pendingUsers = new JSONObject("$myArray");
} catch (JSONException e) {
e.printStackTrace();
}
// Read response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch(Exception e) {
return null;
}
// Convert string to object
try {
jsonObject = new JSONObject(result);
} catch(JSONException e) {
return null;
}
public void getJsonResult(JSONObject pendingRequests)
{
pendingRequests = jsonObject;
}
Here is where I try to receive this and put it into my list
HTTPSendPost postSender = new HTTPSendPost();
postSender.Setup(500, 050, "tesT", htmlUrl);
postSender.execute();
JSONObject pendingRequests = new JSONObject();
postSender.getJsonResult(pendingRequests);
try {
for(int i = 0; i < pendingRequests.length(); i++) {
JSONArray fromUser = pendingRequests.getJSONArray("fromUser");
pendingRequestsArray.add(i, fromUser.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_friends, pendingRequestsArray);
pendingRequestsListView.setAdapter(adapter);
When I try it on my app, I don't get any results on the listView, any help would be appreciated. Sorry for the repost but I have a lot more information and code now. Next time I won't ask a question without the code I have tried.