2016-08-02 4 views
0

Ich versuche, Daten von der mysql Datenbank über Json abzurufen.Wie hole ich Daten aus der Datenbank und zeige sie in ListView an?

das Problem, das ich bin vor ist

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

Hier liest ich meinen kompletten Code bin Entsendung ..

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(); 
     } 
    } 
} 

Jetzt 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; 
    } 

} 

Schließlich mein 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> 

Ich versuchte es bt seine nicht funktioniert, die gleichen Fehler.

+0

Haben Sie die Datenbankdaten auf Ihrer XML-Datei angezeigt werden soll ..? – LvN

+0

Volley bietet Klassen für JSON-Anfragen an - erfahren Sie mehr unter https://developer.android.com/training/volley/ –

+0

ja ... ryt ... ich möchte den Besuch pro Tag zählen und die Nummer in dieser Textansicht anzeigen –

Antwort

0

Laut Fehlermeldung erscheint, dass in Ihrem JSON String Sie haben keine gültige JSON Struktur (<br !!!)

Sie JSON Ausgang auf Serverseite überprüfen müssen nicht in Ihrem Android-Code.

können Sie http://jsonlint.com/ verwenden für validieren Ihre JSON

Verwandte Themen