无法完全从Json获取数据

阿比纳夫·拉贾(Abhinav raja)

我正在从http://abinet.org/?json=1获取JSON数据,并在中显示标题ListView代码可以正常工作,但是问题是,它跳过了我的几个标题,ListView而一个标题正在重复。

您可以通过复制将上面给出的url中的json数据粘贴到JSON编辑器在线http://www.jsoneditoronline.org/中,
我希望将“ posts”数组中的标题显示在中ListView,但是这样显示:

在此处输入图片说明

if you see the JSON data from the link above, its missing like 3 titles (they should come between the first and second title) and 5th title is being repeated. Dont know why this is happening. What minor adjustments i need to do? Please help me.

this is my code :

public class MainActivity extends Activity {



// URL to get contacts JSON
private static String url = "http://abinet.org/?json=1";

// JSON Node names
private static final String TAG_POSTS = "posts";
static final String TAG_TITLE = "title";



private ProgressDialog pDialog;
JSONArray contacts = null;
TextView img_url;


ArrayList<HashMap<String, Object>> contactList;
ListView lv;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.newslist);

    contactList = new ArrayList<HashMap<String, Object>>();

    new GetContacts().execute();
}



private class GetContacts extends AsyncTask<Void, Void, Void> {

     protected void onPreExecute() {
         super.onPreExecute();
         // Showing progress dialog
         pDialog = new ProgressDialog(MainActivity.this);
         pDialog.setMessage("Please wait...");
         pDialog.setCancelable(false);
         pDialog.show();

     }


    protected Void doInBackground(Void... arg0) {



        // Making a request to url and getting response

        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject jsonObj = jParser.getJSONFromUrl(url);


       // if (jsonStr != null) {
            try {


                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_POSTS);




                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                  // JSONObject c = contacts.getJSONObject(i);


                   JSONObject posts = contacts.getJSONObject(i);
                   String title = posts.getString(TAG_TITLE).replace("&#8217;", "'");
                   JSONArray attachment = posts.getJSONArray("attachments");
                     for (int j = 0; j< attachment.length(); j++){
                   JSONObject obj = attachment.getJSONObject(j);
                   JSONObject image = obj.getJSONObject("images");

                   JSONObject image_small = image.getJSONObject("thumbnail");

                   String  imgurl = image_small.getString("url");  


                    HashMap<String, Object> contact = new HashMap<String, Object>();
                    contact.put("image_url", imgurl);

                    contact.put(TAG_TITLE, title);
                    contactList.add(contact);
                     }    

               }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();



        adapter=new LazyAdapter(MainActivity.this, contactList);
        lv.setAdapter(adapter);

    }



   }

 } 

this is my JsonParser class (although its not required):

public JSONParser() {
}
 public JSONObject getJSONFromUrl(String url) {
  // Making HTTP request
  try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    is = httpEntity.getContent();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (ClientProtocolException e) {
   e.printStackTrace();
 } catch (IOException e) {
   e.printStackTrace();
 }
  try {
   BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8);
   StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line + "n");
   }
    is.close();
    json = sb.toString();
  }  catch (Exception e) {
   Log.e("Buffer Error", "Error converting result " + e.toString());
  }
  // try parse the string to a JSON object
  try {
    jObj = new JSONObject(json);
  } catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
 }
 // return JSON String
 return jObj;
}
} 

and this is adapter class:

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, Object>> data;
private static LayoutInflater inflater=null;


public LazyAdapter(Activity a,ArrayList<HashMap<String, Object>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.third_row, null);

    TextView title = (TextView)vi.findViewById(R.id.headline3); // title
    SmartImageView iv =  (SmartImageView) vi.findViewById(R.id.imageicon);


   HashMap<String, Object> song = new HashMap<String, Object>();
    song = data.get(position);

    // Setting all values in listview
    title.setText((CharSequence) song.get(MainActivity.TAG_TITLE));

    iv.setImageUrl((String) song.get("image_url"));
  thumb_image);
    return vi;
}
}

Please help me. I am stuck at this for more than a week now. I think there is just something to be changed in my MainActivity class.

greenapps

Some of the 10 (well today there are 10 ) 'titles' have no 'attachments'. Some have one and some have two. There are only 5 titles wich have one or two images. You only create a new

HashMap<String, Object> contact

如果有附件。(附件是图像)。在进入附件循环之前,您应该创建“联系人”。此外,每个标题只需要一张图像。如果没有附件,则可以将图像设置为null。在getView中,如果图像为空,则从可绘制资源中设置一些图像。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章