Reputation: 2385
I am new in android developing. I faced a error like:- JSON Object cannot be convert in jsonArray:-
My ProductInner.java:
public class ProductInner extends AppCompatActivity {
TextView textView;
ImageView imageView;
LinearLayout linearLayout;
String s,s1;
List<String> mainImage = new ArrayList<>();
List<String> outerlist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_inner);
textView = (TextView)findViewById(R.id.product_inner_page_product_name);
imageView = (ImageView)findViewById(R.id.product_inner_page_product_main_image);
linearLayout = (LinearLayout)findViewById(R.id.sub_images_show_section);
new ProductInnerDisplay().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/product&id=80&key=test123$");
}
public class ProductInnerDisplay extends AsyncTask<String,Void,Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(ProductInner.this);
dialog.show();
}
@Override
protected Void doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status==200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONArray jsonArray = jsonObject.getJSONArray("rproducts");
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
List<String> list = new ArrayList<>();
list.add(jsonObject1.getString("product_id"));
list.add(jsonObject1.getString("thumb"));
list.add(jsonObject1.getString("name"));
list.add(jsonObject1.getString("price"));
list.add(jsonObject1.getString("rating"));
list.add(jsonObject1.getString("reviews"));
list.add(jsonObject1.getString("href"));
Log.d("json parse","");
}
for(int i=0;i<jsonArray1.length();i++){
Log.d("length of",jsonArray1.length()+"");
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
List<String> list = new ArrayList<>();
list.add(jsonObject1.getString("id"));
// list.add(jsonObject1.getString("name"));
s=jsonObject1.getString("name");
list.add(jsonObject1.getString("model"));
list.add(jsonObject1.getString("reward"));
list.add(jsonObject1.getString("points"));
// list.add(jsonObject1.getString("image"));
outerlist.add(jsonObject1.getString("image"));
JSONArray jsonArray2 = jsonObject1.getJSONArray("images");
for(int j=0;j<jsonArray2.length();j++){
List<String> list1 = new ArrayList<>();
Log.d("i am here","");
mainImage.add(jsonArray2.getString(j));
Log.d("i am here next", "");
// list1.add(jsonArray2.getString(j));
}
list.add(jsonObject1.getString("price"));
Log.d("i am here 1", "");
JSONArray jsonArray3 = jsonObject1.getJSONArray("options");
for(int j=0;j<jsonArray3.length();j++){
JSONObject jsonObject2 = jsonArray3.getJSONObject(j);
List<String> list1 = new ArrayList<>();
list1.add(jsonObject2.getString("product_option_id"));
list1.add(jsonObject2.getString("option_id"));
list1.add(jsonObject2.getString("name"));
list1.add(jsonObject2.getString("type"));
JSONArray jsonArray4 = jsonObject2.getJSONArray("option_value");
for(int k=0;k<jsonArray4.length();k++){
JSONObject jsonObject3 = jsonArray4.getJSONObject(k);
List<String> list2 = new ArrayList<>();
list2.add(jsonObject3.getString("product_option_value_id"));
list2.add(jsonObject3.getString("option_value_id"));
list2.add(jsonObject3.getString("name"));
list2.add(jsonObject3.getString("image"));
list2.add(jsonObject3.getString("price"));
list2.add(jsonObject3.getString("price_prefix"));
}
list1.add(jsonObject2.getString("required"));
}
list.add(jsonObject1.getString("minimum"));
list.add(jsonObject1.getString("newprice"));
list.add(jsonObject1.getString("qty"));
list.add(jsonObject1.getString("rating"));
list.add(jsonObject1.getString("description"));
JSONArray jsonArray4 = jsonObject1.getJSONArray("attribute_groups");
for(int j=0;j<jsonArray4.length();j++){
JSONObject jsonObject2 = jsonArray4.getJSONObject(j);
List<String> list1 = new ArrayList<>();
list1.add(jsonObject2.getString("attribute_group_id"));
list1.add(jsonObject2.getString("name"));
JSONArray jsonArray5 = jsonObject2.getJSONArray("attribute");
for(int k=0;k<jsonArray5.length();k++){
JSONObject jsonObject3 = jsonArray5.getJSONObject(k);
List<String> list2 = new ArrayList<>();
list2.add(jsonObject3.getString("attribute_id"));
list2.add(jsonObject3.getString("name"));
list2.add(jsonObject3.getString("text"));
}
}
}
}
}catch (IOException |JSONException e){
Log.e("Error",e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
dialog.dismiss();
// super.onPostExecute(aVoid);
// new SetImageView(image).execute(thumbDiscount.get(j));
textView.setText(s);
new SetImage(imageView).execute(s1);
for(int i=0;i<mainImage.size();i++){
ImageView imageView = new ImageView(ProductInner.this);
new SetImage(imageView).execute(mainImage.get(i));
linearLayout.addView(imageView, i);
}
}
}
public class SetImage extends AsyncTask<String,Void,Bitmap>{
ImageView bitmap;
public SetImage(ImageView bitmap){
this.bitmap = bitmap;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
String urldisplay = params[0];
Bitmap bitmap = null;
try{
InputStream in = new java.net.URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
}catch (IOException e ) {
e.getMessage();
// Log.e("Error :",e.getMessage());
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
bitmap.setImageBitmap(result);
//super.onPostExecute(bitmap);
}
}
}
My Logcat show this error:-
E/Error: Value {"id":"80","name":"Bag_80","model":"
","reward":"0","points":"0","image":"http://opencart.codeniques.com/shopping/image/cache/data/product/bag_81_1-1000x1000.JPG","images":["http://opencart.codeniques.com/shopping/image/cache/data/product/bag_80_2-80x80.JPG"],"price":1175,"minimum":"1","newprice":1163.25,"qty":4,"rating":0,"description":""} at productdata of type org.json.JSONObject cannot be converted to JSONArray`
Upvotes: 0
Views: 3936
Reputation: 2727
You are getting productData as a JSONArray while it's a JsonObject which contains a JsonArray of images as I can see in the json link that you provided
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
that line of code should be a JsonObject
Upvotes: 0
Reputation: 11969
productdata
is not a JSONArray but a JSONObject, so you have to change
JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");
to
JSONObject jsonObject1 = jsonObject.getJSONObjct("productdata");
Upvotes: 4
Reputation: 132982
As in log :
productdata of type org.json.JSONObject cannot be converted to JSONArray
means productdata
key is used for JSONObject
in response json of server instead of JSONArray
.
So get productdata
key value as JSONObject instead of JSONArray :
JSONObject jsonObject1 = jsonObject.getJSONObject("productdata");
Upvotes: 1