我试图通过使用改造库在listview中显示json的结果,代码是正确的,没有任何错误,但它不显示结果....如果有人可以帮助我请。
job_vec.php
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
// $id = $_GET['id'];
require_once('db.php');
$sql = "SELECT * FROM job_entity ";
$r = mysqli_query($connection,$sql);
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,
array('Id'=>$row['id'],
'Title'=>$row['Title'],
'City'=>$row['City'],
'Address'=>$row['Address'],
'Specialization'=>$row['Specialization'],
));
}
echo json_encode(array("result"=>$result));
mysqli_close($connection);
}
?>
&#13;
jobAPI.java
public interface jobAPI {
@GET("job_vec.php")
public void getJobs(Callback<List<job_entity>> response);
}
&#13;
以及在listview中显示结果的方法
package com.example.hamdaalissaei.flah.service;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.hamdaalissaei.flah.InterfaceAPI.jobAPI;
import com.example.hamdaalissaei.flah.R;
import com.example.hamdaalissaei.flah.model.job_entity;
import java.util.List;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
public class Job_vacancy extends AppCompatActivity implements ListView.OnItemClickListener {
public static final String ROOT_URL = "http://192.168.0.120/flah";
public static final String KEY_Title = "Title";
public static final String KEY_City = "City";
public static final String KEY_Address = "Address";
public static final String KEY_Specialization= "Specialization";
private ListView listView;
private List<job_entity> jobs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_vacancy);
listView = (ListView) findViewById(R.id.list);
getJobs();
//Setting onItemClickListener to listview
listView.setOnItemClickListener(this);
}
private void getJobs() {
//While the app fetched data we are displaying a progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);
//Creating a rest adapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL)
.build();
//Creating an object of our api interface
jobAPI api = adapter.create(jobAPI.class);
//Defining the method
api.getJobs(new retrofit.Callback<List<job_entity>>() {
@Override
public void success(List<job_entity> job_entities, retrofit.client.Response response) {
//Dismissing the loading progressbar
loading.dismiss();
//Storing the data in our list
jobs = job_entities;
//Calling a method to show the list
showList();
}
@Override
public void failure(RetrofitError error) {
}
});
}
//Our method to show list
private void showList(){
//String array to store all the book names
String[] items = new String[jobs.size()];
//Traversing through the whole list to get all the names
for(int i=0; i<jobs.size(); i++){
//Storing names to string array
items[i] = jobs.get(i).getTitle();
}
//Creating an array adapter for list view
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.simple_list,items);
//Setting adapter to listview
listView.setAdapter(adapter);
}
//This method will execute on listitem click
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Creating an intent
Intent intent = new Intent(this, jobs.class);
//Getting the requested book from the list
job_entity job = jobs.get(position);
//Adding book details to intent
intent.putExtra(KEY_Title,job.getTitle());
intent.putExtra(KEY_Address,job.getAddress());
intent.putExtra(KEY_City,job.getCity());
intent.putExtra(KEY_Specialization,job.getSpecialization());
//Starting another activity to show book details
startActivity(intent);
}
}
&#13;