2015-12-09 77 views
0

我填充與此JSON字符串單獨的項目文件列表視圖獲取數據的陣列中的機器人從MySQL JSON ...使用凌空

項目文件ticket_items 列表視圖文件是ticktsfr

Tickets.php:

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){ 

$user_id = $_GET['user_id']; 
require_once('db_configuration.php'); 
$sql = "SELECT * FROM (SELECT a.event_name, b.ticket_name,b.ticket_quantity, 
b.ticket_description, b.ticket_channel, b.ticket_start_date, 
b.ticket_end_date,b.user_id FROM event a, ticket b WHERE a.event_id = 
b.event_id and a.user_id = b.user_id) AS T WHERE user_id='"$user_id"'"; 

$res = mysqli_query($con,$sql); 

$result = array(); 

while($row = mysqli_fetch_array($res)) 
{ 
array_push($result,array 
(
"event_name"=>$row[0], 
"ticket_name"=>$row[1], 
"ticket_quantity"=>$row[2], 
"ticket_description"=>$row[3], 
"ticket_channel"=>$row[4], 
"ticket_start_date"=>$row[5], 
"ticket_end_date"=>$row[6], 
"user_id"=>$row[7] 
) 
); 
} 
    echo json_encode(array("result"=>$result)); 
    mysqli_close($con); 
} 

Tickets.java:(Activity)

public class Tickets extends Fragment { 
//boolean variable to check user is logged in or not 
//initially it is false 
boolean loggedIn = false; 
private ListView listView; 

private static final String JSON_URL = 
"http://myip/eevento/tickets.php?user_id="; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.ticketsfr, null); 
    listView = (ListView) view.findViewById(R.id.list3); 

    sendRequest(); 
    return view; 
} 

private void sendRequest(){ 

    String url = JSON_URL+Loginhandler.USER_ID; 
    StringRequest stringRequest = new StringRequest(url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        showJSON(response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error){ 
Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 
    requestQueue.add(stringRequest); 
} 

private void showJSON(String json) { 
    Ticketshandler2 pj = new Ticketshandler2(json); 
    pj.parseJSON(); 
     Ticketshandler cl = new Ticketshandler(getActivity(), 
Ticketshandler2.event_name, Ticketshandler2.ticket_name, 
Ticketshandler2.ticket_quantity, Ticketshandler2.ticket_description, 
Ticketshandler2.ticket_channel, Ticketshandler2.ticket_start_date, 
Ticketshandler2.ticket_end_date, Ticketshandler2.user_id); 
     listView.setAdapter(cl); 

} 
} 

個Ticketshandler.java:(Class)

public class Ticketshandler extends ArrayAdapter<String> { 
TextView namme; 

ProgressDialog loading; 



private String[] event_name; 
private String[] ticket_name; 
private String[] ticket_quantity; 
private String[] ticket_description; 
private String[] ticket_channel; 
private String[] ticket_start_date; 
private String[] ticket_end_date; 
private String[] user_id; 
private Activity context; 

public Ticketshandler(Activity context, String[] event_name, String[] 
ticket_name,String[] ticket_quantity,String[] ticket_description,String[] 
ticket_channel,String[] ticket_start_date,String[] ticket_end_date,String[] 
user_id) { 
    super(context, R.layout.tickets_item, event_name); 
    this.context = context; 
    this.event_name = event_name; 
    this.ticket_name = ticket_name; 
    this.ticket_quantity = ticket_quantity; 
    this.ticket_description = ticket_description; 
    this.ticket_channel = ticket_channel; 
    this.ticket_start_date = ticket_start_date; 
    this.ticket_end_date = ticket_end_date; 
    this.user_id = user_id; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View listViewItem = inflater.inflate(R.layout.tickets_item, null, true); 
    TextView event_topic_description = (TextView) 
listViewItem.findViewById(R.id.titem); 

    String output = "" + event_name[position] + "\n" + ticket_name[position] 
+ "\n" + ticket_quantity[position] + "\n" + ticket_description[position] + 
"\n" 
+ ticket_channel[position] + "\n" + ticket_start_date[position] + "\n" + 
ticket_end_date[position]; 
// String output = "" +event_name[position]; 
     event_topic_description.setText(output); 
    System.out.println(output); 

     return listViewItem; 
} 

}

Ticketshandler2.java:(Class)

public class Ticketshandler2 { 
public static String[] event_name; 
public static String[] ticket_name; 
public static String[] ticket_quantity; 
public static String[] ticket_description; 
public static String[] ticket_channel; 
public static String[] ticket_start_date; 
public static String[] ticket_end_date; 
public static String[] user_id; 

public static final String JSON_ARRAY = "result"; 
public static final String EVENT_NAME = "event_name"; 
public static final String TICKET_NAME = "ticket_name"; 
public static final String TICKET_QUANTITY = "ticket_quantity"; 
public static final String TICKET_DESCRIPTION = "ticket_description"; 
public static final String TICKET_CHANNEL = "ticket_channel"; 
public static final String TICKET_START_DATE = "ticket_start_date"; 
public static final String TICKET_END_DATE = "ticket_end_date"; 
public static final String USER_ID = "user_id"; 

private JSONArray users = null; 

private String json; 

public Ticketshandler2(String json){ 
    this.json = json; 
} 

protected void parseJSON(){ 
    JSONObject jsonObject=null; 
    try { 
     jsonObject = new JSONObject(json); 
     users = jsonObject.getJSONArray(JSON_ARRAY); 
     System.out.println(users); 
     event_name = new String[users.length()]; 
     ticket_name = new String[users.length()]; 
     ticket_quantity = new String[users.length()]; 
     ticket_description = new String[users.length()]; 
     ticket_channel = new String[users.length()]; 
     ticket_start_date = new String[users.length()]; 
     ticket_end_date = new String[users.length()]; 
     user_id = new String[users.length()]; 

     for(int i=0;i<users.length();i++){ 
      JSONObject jo = users.getJSONObject(i); 
       event_name[i] = jo.getString(EVENT_NAME); 
       ticket_name[i] = jo.getString(TICKET_NAME); 
       ticket_quantity[i] = jo.getString(TICKET_QUANTITY); 
       ticket_description[i] = jo.getString(TICKET_DESCRIPTION); 
       ticket_channel[i] = jo.getString(TICKET_CHANNEL); 
       ticket_start_date[i] = jo.getString(TICKET_START_DATE); 
       ticket_end_date[i] = jo.getString(TICKET_END_DATE); 
       user_id[i] = jo.getString(USER_ID); 

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

登錄:

"12-09 12:00:17.750 31591-31591/com.example.dell.evento W/System.err: 
org.json.JSONException: Value <br of type java.lang.String cannot be 
converted to JSONObject" 

我想我的查詢有一些問題,但它是完美的工作,在wamp mysql分開..我不明白是什麼問題,,幫助...

+0

首先檢查您實際從服務器獲得的回覆。 –

+0

響應:{「result」:[{「event_name」:「evento」,「ticket_name」:「RSVP」,「ticket_quantity」:「12」,「ticket_description」:「test」,「ticket_channel」:「test」 「ticket_start_date」:「2015-11-03 06:03:42 PM」,「ticket_end_date」:「2015-11-03 06:03:42 PM」,「user_id」:「80」}]} –

+0

這是一個有效的JSON! –

回答

0

您的回覆代碼可能無法從htmlentities中跳出。 您需要先將html字符串轉義爲正確的字符代碼。因爲java默認不會接收任何字符代碼。

+0

以下是響應:{「result」:[{「event_name」:「evento」,「ticket_name」:「RSVP」,「ticket_quantity」:「12」,「ticket_description」:「test」,「ticket_channel」 test「,」ticket_start_date「:」2015-11-03 06:03:42 PM「,」ticket_end_date「:」2015-11-03 06:03:42 PM「,」user_id「:」80「}]} –