解析JSON并将其显示到自定义listview android中

时间:2014-03-29 09:07:10

标签: java android json parsing listview

我目前正在开发一个需要解析在线JSON并将其显示到自定义列表视图中的Android应用程序。

我的问题是,当我运行应用程序时,它有一个错误说

org.json.JSONException: End of input at character 0

这就是我的所作所为:

*的布局/ row_listitem.xml *

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:id="@+id/iv_icon_social"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_centerVertical="true"
    android:background="@drawable/image1"
    android:src="@drawable/image1"
    android:visibility="gone" />

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="fill_parent"
    android:layout_height="85dp"
    android:layout_alignParentRight="true"
    android:layout_marginTop="0dp"
    android:layout_toRightOf="@+id/iv_icon_social"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="5dip"
    android:visibility="visible" >

    <TextView
        android:id="@+id/txt_ttlsm_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="Sample text"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txt_ttlcontact_row2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="3dp"
        android:paddingLeft="10dp"
        android:maxEms="20"
        android:maxLines="2"
        android:singleLine="false"
        android:ellipsize="end"
        android:text="Sample text2"
        android:textColor="#808080"
        android:textSize="15dp"
        android:textStyle="normal"
        android:visibility="visible" />


</LinearLayout>

</RelativeLayout>

*的布局/ activity_main.xml中*

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" >

    </ListView>

</RelativeLayout>

MyBaseAdapter.java

package com.example.parsing_online;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyBaseAdapter extends BaseAdapter {

    private Activity activity;
    private static ArrayList title,notice;
    private static LayoutInflater inflater = null;


    public MyBaseAdapter(Activity a, ArrayList b, ArrayList bod) {
        activity = a;
        this.title = b;
        this.notice=bod;

        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (convertView == null){
            vi = inflater.inflate(R.layout.row_listitem, null);
        }

        TextView title2 = (TextView) vi.findViewById(R.id.txt_ttlsm_row); // title
        String song = title.get(position).toString();
        title2.setText(song);


        TextView title22 = (TextView) vi.findViewById(R.id.txt_ttlcontact_row2); // notice
        String song2 = notice.get(position).toString();
        title22.setText(song2);



        return vi;
    }

}

并在我的 MainActivity.java

 package com.example.parsing_online;

    import java.io.IOException;
    import java.util.ArrayList;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.widget.ListView;



    public class MainActivity extends Activity {

    ArrayList<String> title_array = new ArrayList<String>();
    ArrayList<String> notice_array = new ArrayList<String>();
    ListView list;
    MyBaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.listView1);
        new TheTask().execute();
    }

    class TheTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
          String str = null;
          try {
                HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost(
                        "http://******.com/api/v1/***");
             httppost.addHeader("Authorization", "Token token=Wa5sfwP3ku7c15qkZTsdSA");
             HttpResponse response = httpclient.execute(httppost);
             str = EntityUtils.toString(response.getEntity());
          } catch (IOException e) {
                // TODO Auto-generated catch block
             e.printStackTrace();
          }
          return str;
         }



         @Override
         protected void onPostExecute(String result) {
          super.onPostExecute(result);
          String response = result.toString();
          try {
            JSONArray new_array = new JSONArray(response);

            for (int i = 0, count = new_array.length(); i < count; i++) {
                try {
                    JSONObject jsonObject = new_array.getJSONObject(i);
                    title_array.add(jsonObject.getString("title").toString());
                    notice_array.add(jsonObject.getString("notice").toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            adapter = new MyBaseAdapter(MainActivity.this, title_array, notice_array);
            list.setAdapter(adapter);
           } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }

        }
    }

  }

1 个答案:

答案 0 :(得分:0)

检查一下json。我认为它不是一个有效的。只需尝试下面的一个

{
"id": "4",
"title": "Test",
"notice": "Test"
}