Okay J'ai cette application simple pour afficher les données de json..unfortunatelly chaque fois que je lance l'application, il a fermé immédiatement.Android Volley Afficher les données JSON
J'ai une amende api travailler dans mon serveur
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
mysql_connect("localhost","k4371034_android","android123");
mysql_select_db("k4371034_android");
switch($_GET['case']){
case 'sekolah':
$sql = "SELECT * FROM lokasi_checkin";
$r = mysql_query($sql);
$return_arr = array();
while($row = mysql_fetch_array($r)){
$row_array['id'] = $row['id'];
$row_array['image'] = $row['image'];
$row_array['nama'] = $row['nama'];
$row_array['long'] = $row['long'];
$row_array['lat'] = $row['lat'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
die();
break;
case 'user':
$sql = "SELECT * FROM user";
$r = mysql_query($sql);
$return_arr = array();
while($row = mysql_fetch_array($r)){
$row_array['id'] = $row['id'];
$row_array['user'] = $row['user'];
$row_array['image'] = $row['image'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
die();
break;
}
}
Il sortie très bien.
En Java lui-même.
public class MainActivity extends AppCompatActivity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://www.lineitopkal.com/android/api.php?case=user";
private ProgressDialog pDialog;
private List<School> schoolList = new ArrayList<School>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, schoolList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
//Creating volley request obj
JsonArrayRequest schoolReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
School school = new School();
school.setThumbnailUrl(obj.getString("image"));
school.setName(obj.getString("user"));
// adding school to school array
schoolList.add(school);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(schoolReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
}
peut pas voir ce qui cause le problème cuz mon studio Android n'a pas dit exactement ce que le problème est .... il n'a rien montré dans Logcat.
Toute réponse sera vraiment appréciée. Merci u
EDIT
D'accord, il se trouve que le problème vient de ma classe AppController ... ce qui donne NullPointerException.
La syntaxe
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
essayer d'imprimer une réponse dans le journal d'abord, pour vérifier si votre php envoie des données ou non. –