如何从数据库中获取数据并将其显示在ListView上?

时间:2016-08-02 08:19:12

标签: android mysql json database fetch

我正在尝试通过json从mysql数据库中获取数据。

我面临的问题是

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

我在这里发布完整的代码..

Visit.Java

   public class Visit extends TabFragment {
    public Visit(){};

    JSONObject jsonobject;
    JSONArray ownerObj;
    ListView listview;

    ListViewAdapter3 listadapter;
    ArrayList<HashMap<String, String>> arraylist;
    String uname = "null";
    SessionManager session;
    private static String url_visitor = "http://10.0.2.2/portal/fetchinforder.php";

    JSONParser jParser = new JSONParser();

    ArrayList<String> itemwod = new ArrayList<String>();
    ArrayList<String> itemorder = new ArrayList<String>();
    ArrayList<String> item_remark= new ArrayList<String>();
    ArrayList<String> o_username= new ArrayList<String>();
    View view;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard

        session = new SessionManager(getActivity().getApplicationContext());
        // get user data from session
        HashMap<String, String> user = session.getUserDetails();
        // name
        String name = user.get(SessionManager.KEY_NAME);
        // email
        String email = user.get(SessionManager.KEY_EMAIL);

        View view = inflater.inflate(R.layout.activity_visit, container, false);
        getActivity().setTitle("Visit");
        listview = (ListView) view.findViewById(R.id.lvvisit);
        new DownloadJSON().execute();
        return  view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //add the values which need to be saved from the drawer to the bundle
        // outState = result.saveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            //mProgressDialog = new ProgressDialog(getActivity());
           // mProgressDialog.setTitle("Please Wait");
            // Set progressdialog message
            //mProgressDialog.setMessage("Loading...");
            //mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            //mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... args) {
            // Create an array
            try {
                arraylist = new ArrayList<HashMap<String, String>>();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", uname));
                JSONObject json = jParser.makeHttpRequest(url_visitor, "POST", params);

                int success1 = Integer.parseInt(json.getString("success3"));
                Log.d("success3", json.toString());

                if (success1 == 0)
                {
                    Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
                }
                if (success1 == 1) {
                    ownerObj = json.getJSONArray("Ordera");
                    // arraylist = new ArrayList<HashMap<String, String>>();
                    // Retrieve JSON Objects from the given URL address
                    // Locate the array name in JSON
                    // jsonarray = jsonobject.getJSONArray("images");

                    for (int i = 0; i < ownerObj.length(); i++) {
                        //  HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = ownerObj.getJSONObject(i);
                        // Retrive JSON Objects
                        if (jsonobject.getString("username").equalsIgnoreCase(uname))
                        {
                            itemwod.add(jsonobject.getString("itemwod"));
                            item_remark.add(jsonobject.getString("itemremark"));
                            itemorder.add(jsonobject.getString("itemorder"));
                            o_username.add(jsonobject.getString("o_username"));
                            }
                        }
                    }
                }
            catch (Exception e)
            {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml

            listview = (ListView) getActivity().findViewById(R.id.lvvisit);
            listadapter = new ListViewAdapter3(getActivity(), itemwod,item_remark,itemorder,o_username);
            // Set the adapter to the ListView
            listview.setAdapter(listadapter);
            // Close the progressdialog

           // mProgressDialog.dismiss();
        }
    }
}

现在, ListViewAdapter3.Java

    package com.example.sachin.omcommunication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class ListViewAdapter3 extends BaseAdapter {
    Context cntx;

    ArrayList<String> itemwod = new ArrayList<String>();
    ArrayList<String> itemorder = new ArrayList<String>();
    ArrayList<String> item_remark= new ArrayList<String>();
//    ArrayList<String> item_date = new ArrayList<String>();

    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;


    public ListViewAdapter3(Context context,
                            ArrayList<String> itm_wod,
                            ArrayList<String> itm_order,
                            ArrayList<String> itm_remark,
                            ArrayList<String> o_username
    ) {
        // TODO Auto-generated constructor stub
        cntx = context;


        itemwod = itm_wod;
        itemorder = itm_order;
        item_remark = itm_remark;
        o_username = o_username;


    }

    @Override
    public int getCount() {
        return itemorder.size();
    }

    @Override
    public Object getItem(int position) {
        return itemorder.get(position);
    }

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

    @SuppressWarnings("deprecation")
    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView lvtaskname,lvtaskdetails,lvtaskremark,lvtaskdate;

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

        LayoutInflater inflater = LayoutInflater.from(cntx);

        convertView = inflater.inflate(R.layout.raw_order, parent,
                false);


        lvtaskname= (TextView) convertView.findViewById(R.id.lvtaskname);
        lvtaskdetails = (TextView) convertView.findViewById(R.id.lvtaskdetails);
        lvtaskremark = (TextView) convertView.findViewById(R.id.lvtaskremark);
        //lvtaskdate = (TextView) convertView.findViewById(R.id.lvtaskdate);

        lvtaskname.setText(itemwod.get(position));
        lvtaskdetails.setText(itemorder.get(position));
        lvtaskremark.setText(item_remark.get(position));
       //lvtaskdate.setText(item_date.get(position));


        return convertView;
    }

}

最后我的布局

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="10dp"
                    android:layout_weight="73"
                    android:orientation="vertical">

                    <ListView
                        android:id="@+id/lvvisit"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
             </LinearLayout>

我尝试过它没有工作得到同样的错误。

1 个答案:

答案 0 :(得分:0)

根据错误消息,您的JSON字符串中似乎没有有效的JSON结构(<br !!!)

您必须检查服务器端的JSON输出,而不是Android代码。

您可以使用http://jsonlint.com/来验证JSON