Php 无法使用截取从数据库检索数据
我想在MySQL中从一个表中检索多个元组。我也在用截击。我想将接收到的数据附加到textview。我截击了,服务器出错。 截取和JSon请求的代码如下Php 无法使用截取从数据库检索数据,php,android,mysql,json,android-volley,Php,Android,Mysql,Json,Android Volley,我想在MySQL中从一个表中检索多个元组。我也在用截击。我想将接收到的数据附加到textview。我截击了,服务器出错。 截取和JSon请求的代码如下 private void active1(){ StringRequest stringRequest = new StringRequest(Request.Method.POST, JSON_URL, new Response.Listener<String>() {
private void active1(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(bossmain.this, response, Toast.LENGTH_LONG).show();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(bossmain.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,usernameb);
params.put(KEY_ACTIVE,active);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("workers");
for(int i=0;i<result.length();i++) {
JSONObject worker = result.getJSONObject(i);
name = worker.getString("username");
lv.append(name+"\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void active1(){
StringRequest StringRequest=新的StringRequest(Request.Method.POST,JSON_URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Toast.makeText(bossmain.this,response,Toast.LENGTH_LONG.show();
showJSON(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(bossmain.this,error.toString(),Toast.LENGTH_LONG.show();
}
}){
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(KEY_用户名,usernameb);
参数put(按键激活,激活);
返回参数;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
}
私有void showJSON(字符串响应){
字符串名称=”;
试一试{
JSONObject JSONObject=新JSONObject(响应);
JSONArray result=jsonObject.getJSONArray(“workers”);
对于(inti=0;i,尝试将PHP API代码更改为:
$res = array();
while($row = mysqli_fetch_array($sql)){
array_push($res, array(
"username"=>$row['workers']['username']
));
}
echo json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_ERROR_UTF8);
您的showJSON方法类似于:
private void showJSON(String response){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("workers");
workers = new String[result.length()];
for(int i=0;i<result.length();i++) {
JSONObject worker = result.getJSONObject(i);
workers[i] = worker.getString("username");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void showJSON(字符串响应){
JSONObject JSONObject=null;
试一试{
jsonObject=新的jsonObject(响应);
JSONArray result=jsonObject.getJSONArray(“workers”);
workers=新字符串[result.length()];
对于(int i=0;i
private void showJSON(String response){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("workers");
workers = new String[result.length()];
for(int i=0;i<result.length();i++) {
JSONObject worker = result.getJSONObject(i);
workers[i] = worker.getString("username");
}
} catch (JSONException e) {
e.printStackTrace();
}
}