توسط
0212121
سلام
من یک برنامه برای فراخوانی json از ووکامرس نوشتم که از کتابخانه volly و RecyclerView استفاده کردم و باتوابع json arrey و jsonobject اونها رو فراخوانی کردم اما بعد از اجذای برنامه فقط صفحه لودینگ میاد و بقیه اطلاعات رو لود نمیکنه نمیدونم مشکل از کجاست
کد کامل اکتیویتی:
package com.example.fresh.freshlandshop;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//Creating a List of superheroes
private List<SuperHeroes> listSuperHeroes;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//Initializing our superheroes list
listSuperHeroes = new ArrayList<>();
//Calling method to get data
getData();
}
//This method will get data from the web api
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"در حال دریافت اطلاعات" ,"لطفا صبر کنید...",false,false);
//Creating a json array request
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(config.DATA_URL,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Dismissing progress dialog
loading.dismiss();
try {
JSONArray obj_products =
response.getJSONArray("products");
parseData(response.getJSONArray("products"));
}
catch(Exception e)
{
}
//calling method to parse json array
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array){
for(int i =0; i<array.length(); i++) {
SuperHeroes superHero = new SuperHeroes();
JSONObject json = null;
try {
json = array.getJSONObject(i);
superHero.setName(json.getString(config.TAG_NAME));
superHero.setRank(json.getString(config.TAG_RANK));
superHero.setRealName(json.getString(config.TAG_REAL_NAME).toString() .replace("</p>","") .replace("</p>",""));
JSONArray products = json.getJSONArray(config.TAG_IMAGE);
String image_url="";
for(int k=0;k<products.length();k++) {
JSONObject new_json = products.getJSONObject(k);
image_url = new_json.getString(config.TAG_IMAGE_URL);
}
superHero.setImageUrl(image_url);
ArrayList<String> addList = new ArrayList<String>();
JSONArray addArray = json.getJSONArray(config.TAG_IMAGE);
int size = addArray.length();
} catch (JSONException e) {
e.printStackTrace();
}
listSuperHeroes.add(superHero);
}
//Finally initializing our adapter
adapter = new cardadapter(listSuperHeroes, this);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setHasFixedSize(true);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
}
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .