【发布时间】:2016-08-01 12:40:59
【问题描述】:
我已经编写了一个 php 代码来从 MYSQL 获取数据数组,但现在我正在努力将它恢复到我的 android 并只是为了祝酒。
这是我的 php 代码:
if($_SERVER['REQUEST_METHOD']=='GET'){
$user_id = $_GET['user_id'];
require_once('dbConnect.php');
$user_tags = array();
foreach ($_REQUEST['user_id'] as $key => $val) {
$user_tags[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}
$user_ids = "'" . implode("','", $user_tags) . "'";
$sql = "SELECT * FROM user_tags WHERE user_id IN ({$user_ids})";
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
$result['tags'][] = $row['tags'];
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
}
这里是代码的 URL:“http://allwaysready.16mb.com/Cuboid/tagsTest.php?user_id[]=7”
这里是 php 代码的结果: {"result":{"tags":["Pascol","PHP","Python"]}}
我想在我的 android 中得到这个“结果”。
这是我的安卓代码:
public class Jobs extends AppCompatActivity {
public static String tags;
private String JSON_STRING;
public static final String TAG_JSON_ARRAY="result";
public static final String KEY_USER_TAGS="tags";
public static final String URL_GET_TAG = "http://allwaysready.16mb.com/Cuboid/tagsTest.php?user_id[]=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jobs);
getJSON();
Toast.makeText(getApplicationContext(),tags,Toast.LENGTH_LONG).show();
}
private void showEmployee() {
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(TAG_JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
tags = jo.getString(KEY_USER_TAGS);
HashMap<String, String> employees = new HashMap<>();
employees.put(KEY_USER_TAGS, tags);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Jobs.this,"Fetching Data","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(URL_GET_TAG);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
但我只得到空白吐司。如果我犯了任何错误,请帮助我。
【问题讨论】: