简体   繁体   English

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

[英]How to fetch a data from the database and display it on ListView?

i am trying to fetch a data from the mysql database through json. 我试图通过json从mysql数据库中获取数据。

the problem i am facing is 我面临的问题是

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

Here i am posting my complete code.. 我在这里发布我的完整代码。

Visit.Java 访问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();
        }
    }
}

Now, ListViewAdapter3.Java 现在, 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;
    }

}

Lastly my layout 最后我的布局

            <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>

i tried it bt its not working getting the same error. 我尝试过它无法正常工作得到同样的错误。

According to error message, seems that in your JSON string you have not a valid JSON structure ( <br !!!) 根据错误消息,似乎您的JSON字符串中没有有效的JSON结构( <br !!!)

You must check JSON output on server side not in your Android code. 您必须在服务器端而不是在Android代码中检查JSON输出。

You can use http://jsonlint.com/ for validate your JSON 您可以使用http://jsonlint.com/来验证JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM