基于用户名检索android上的json数据

I am new to android programming and right now I am having trouble trying to retrieve data from mysql phpmyadmin based on the username that is logged into the app. I have a table called booking that has details of all the user. now I want to retrieve all the details of one particular user from the database. how do I do that?

bookinglist.java

public class bookinglist extends ArrayAdapter<String> {
private String[] sportcenter_name;
private static String[] facility_name;
private static String[] date;
private static String[] start_time;
private static String[] end_time;
private Activity context;

public bookinglist(Activity context, String[] sportcenter_name, String[] facility_name, String[] date, String[] start_time, String[] end_time) {
    super(context, R.layout.list_item, sportcenter_name);
    this.context = context;
    this.sportcenter_name = sportcenter_name;
    this.facility_name = facility_name;
    this.date = date;
    this.start_time = start_time;
    this.end_time = end_time;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.list_booking, null, true);
    TextView textViewspname = (TextView) listViewItem.findViewById(R.id.textViewspname);
    TextView textViewFacility = (TextView) listViewItem.findViewById(R.id.textViewFacility);
    TextView textViewdate = (TextView) listViewItem.findViewById(R.id.textViewdate);
    TextView textViewstarttime = (TextView) listViewItem.findViewById(R.id.textViewstarttime);
    TextView textViewendtime = (TextView) listViewItem.findViewById(R.id.textViewendtime);

    textViewspname.setText(sportcenter_name[position]);
    textViewFacility.setText(facility_name[position]);
    textViewdate.setText(date[position]);
    textViewstarttime.setText(start_time[position]);
    textViewendtime.setText(end_time[position]);

    return listViewItem;
}
}

bookiglistJSON.java

public class bookinglistJSON {
public static String[] sportcenter_name;
public static String[] facility_name;
public static String[] date;
public static String[] start_time;
public static String[] end_time;

public static final String JSON_ARRAY = "result";
public static final String KEY_Sportcenter = "sportcenter_name";
public static final String KEY_Facility = "facility_name";
public static final String KEY_Date = "date";
public static final String KEY_Starttime = "start_time";
public static final String KEY_Endtime = "end_time";
private JSONArray booking = null;
private String json;

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

protected void bookinglistJSON(){
    JSONObject jsonObject=null;
    try {
        jsonObject = new JSONObject(json);
        booking = jsonObject.getJSONArray(JSON_ARRAY);
        sportcenter_name = new String[booking.length()];
        facility_name = new String[booking.length()];
        date = new String[booking.length()];
        start_time = new String[booking.length()];
        end_time = new String[booking.length()];

        for(int i=0;i<booking.length();i++){
            JSONObject jo = booking.getJSONObject(i);
            sportcenter_name[i] = jo.getString(KEY_Sportcenter);
            facility_name[i] = jo.getString(KEY_Facility);
            date[i] = jo.getString(KEY_Date);
            start_time[i] = jo.getString(KEY_Starttime);
            end_time[i] = jo.getString(KEY_Endtime);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

ViewBookings.java

public class ViewBookings extends AppCompatActivity {
public static final String JSON_URL = "http://www.khaledz.com/projects/project/viewbooking.php?username=";
public static final String KEY_USERNAME="username";
private TextView textView;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_booking);
    textView = (TextView) findViewById(R.id.textViewUserName);

    //Fetching email from shared preferences
    SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
    String username = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");

    //Showing the current logged in email to textview
    textView.setText("Current User: " + username);
    listView = (ListView) findViewById(R.id.listViewBooking);
    sendRequest();
}

private void sendRequest(){
    final String username = textView.getText().toString().trim();
    StringRequest stringRequest = new StringRequest(JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ViewBookings.this, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<String, String>();
        map.put(KEY_USERNAME, username);
        return map;
    }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String json){
    bookinglistJSON pj = new bookinglistJSON(json);
    pj.bookinglistJSON();
    bookinglist nl = new bookinglist(ViewBookings.this, bookinglistJSON.sportcenter_name,bookinglistJSON.facility_name,bookinglistJSON.date,bookinglistJSON.start_time,bookinglistJSON.end_time);
    listView.setAdapter(nl);
}
}

php script.

<?php
define('HOST','alahlitimercom.ipagemysql.com');
define('USER','book_play_123');
define('PASS','book_play_123');
define('DB','book_play_123');

$con = mysqli_connect(HOST,USER,PASS,DB);
$username = $_GET['username'];
$sql = "select * from booking WHERE username='$username'";
$res = mysqli_query($con,$sql);

$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'sportcenter_name'=>$row[1],
'facility_name'=>$row[2],
'username'=>$row[3],
'created_at'=>$row[4],
'date'=>$row[5],
'start_time'=>$row[6],
'end_time'=>$row[7],
'court'=>$row[8],
'price'=>$row[9]
));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

?>
dreamwind1985
dreamwind1985 任何想法如何检索特定用户名的详细信息?我已经从post更改为获取我的PHP脚本。现在我在java代码中要做什么?TQ
接近 5 年之前 回复
doudi5524
doudi5524 我想你尝试用$_POST在PHP中检索GET请求,因为我没有看到你在你的android代码中指定post
接近 5 年之前 回复

1个回答



http://afzaln.com/volley/com/android/volley/toolbox/StringRequest.html </ p>

指定默认请求为 GET </ code> </ p>

但是在你的PHP代码中你写了</ p>

  $ username = $ _POST ['username']; 
< / code> </ pre>

您应该将用户名添加为URL参数,使其成为 GET </ code>参数,如下所示:</ p>

 < 代码> http://www.khaledz.com/projects/project/viewbooking.php?username=Harry
</ code> </ pre>

在PHP中你写道:</ p> \ n

  $ username = $ _GET ['username']; 
</ code> </ pre>
</ div>

展开原文

原文

http://afzaln.com/volley/com/android/volley/toolbox/StringRequest.html

Specifies the default request is GET

However in your php code you write

$username = $_POST['username'];

You should add the username as URL paramater to make it a GET parameter like this:

http://www.khaledz.com/projects/project/viewbooking.php?username=Harry

In PHP you write:

$username = $_GET['username'];

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐