解析JSON并添加到ListView

时间:2015-03-16 18:55:02

标签: android json listview

我想更新一个我用一些代码写的不起作用的线程。我试图解析我的信息,在发送邮件请求后,看起来像这样[{“fromUser”:“Andrew”},{“fromUser”:“Jimmy”}]

然后,我想接受这些用户,并将它们添加到列表视图中。 这是我发送HTTPpost的代码,然后是我的代码,用于尝试解析并将其放入适配器。

String temp = "";
String arr[];
          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();
                System.out.print(result);
            } catch (Exception e) {
                return null;
            }

            // Convert string to object
            try {
                JSONArray jArray = new JSONArray(result);
                int count = jArray.length();

                for(int i = 0; i < count; i++)
                {
                    JSONObject json_data = jArray.getJSONObject(i);
                    temp += json_data.getString("fromUser") + ":";
                }

                arr = temp.split(":");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return "Updated";
    }
        public String[] getStringForAdapter()
        {
            return arr;
        }
}

这是我尝试接收此内容并将其放入我的列表

的地方
 HTTPSendPost postSender = new HTTPSendPost();
        postSender.Setup(500, 050, "tesT", htmlUrl);
        postSender.execute();

        String[] pendingRequestArray = postSender.getStringForAdapter();


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_friends, pendingRequestArray);

        pendingRequestsListView.setAdapter(adapter);

当我在我的应用程序上尝试它时,我没有在listView上得到任何结果,任何帮助将不胜感激。很抱歉转贴,但我现在有更多的信息和代码。没有我试过的代码,下次我不会问一个问题。我收到了错误

java.lang.RuntimeException: Unable to start activity
 ComponentInfo{com.skyrealm.brockyy.findmypeepsapp
         /com.skyrealm.brockyy.findmypeepsapp.FriendsActivity}: 
 java.lang.NullPointerException: storage == null

0 个答案:

没有答案