JMB JMB -4 years ago 170
Java Question

Hi guys I am getting the following error: UIL doesn't support scheme(protocol) by default

I am trying to load images from an api into a custom listview but I am receiving this error
:

> E/ImageLoader: UIL doesn't support scheme(protocol) by default
> [{"90":"https:\/\/lh3.googleusercontent.com\/0PhsJWJq9gWsJhfQLbAfZBQC1A5af8XFJ3czzoW1IZ9co13S14wUnkmMqzeJ54CFmCCQ0k9OaY1pi-bNinmRaqk=s90-c"}].
> You should implement this support yourself
> (BaseImageDownloader.getStreamFromOtherSource(...))


This is the code I am currently running.

package com.example.jeffb.myapplicationtest;


import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.Rating;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.jeffb.myapplicationtest.models.RecipeModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ContentLengthInputStream;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.utils.IoUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

static final String API_URL = "http://api.yummly.com/v1/api/recipes?_app_id=6e474ff5&_app_key=dc48611378c79997b39cf477b63f0270&q=Chicken&maxResult=50&start=50";
private ProgressDialog dialog;
private ListView lvRecipes;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config); // Do it on Application start



dialog = new ProgressDialog(this);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading. Please wait..."); // showing a dialog for loading the data


lvRecipes = (ListView)findViewById(R.id.lvRecipes);

Button queryButton = (Button) findViewById(R.id.queryButton);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute(API_URL);
}
});
}

public class JSONTask extends AsyncTask<String,String, List<RecipeModel>>{

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}


@Override
protected List<RecipeModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}

String finalJson = buffer.toString();

JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("matches");


List<RecipeModel> recipeModelList = new ArrayList<>();


for (int i = 0; i < parentArray.length(); i++) {

JSONObject finalObject = parentArray.getJSONObject(i);

RecipeModel recipeModel = new RecipeModel();
recipeModel.setRecipeName(finalObject.getString("recipeName"));
recipeModel.setRating((float)finalObject.getDouble("rating"));
recipeModel.setImageUrlsBySize(finalObject.getString("imageUrlsBySize"));


recipeModelList.add(recipeModel);


}

return recipeModelList;


} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

catch (JSONException e) {
e.printStackTrace();
}

finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

@Override
protected void onPostExecute(List<RecipeModel> result) {
super.onPostExecute(result);

RecipeAdapter adapter = new RecipeAdapter(getApplicationContext(), R.layout.row, result);
lvRecipes.setAdapter(adapter);

dialog.dismiss();
if(result != null) {

}

}

public class RecipeAdapter extends ArrayAdapter {

private List<RecipeModel> recipeModelList;
private int resource;
private LayoutInflater inflater;
public RecipeAdapter(Context context, int resource, List<RecipeModel> objects) {
super(context, resource, objects);
recipeModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

}



@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null) {
convertView = inflater.inflate(resource, null);
}

ImageView icon;
TextView RecipeName;
RatingBar ratingBar;

icon = (ImageView)convertView.findViewById(R.id.Icon);
RecipeName = (TextView)convertView.findViewById(R.id.RecipeName);
ratingBar = (RatingBar)convertView.findViewById(R.id.ratingBar);



// Then later, when you want to display image
ImageLoader.getInstance().displayImage(recipeModelList.get(position).getImageUrlsBySize(), icon);
// Default options will be used

RecipeName.setText(recipeModelList.get(position).getRecipeName());


ratingBar.setRating(recipeModelList.get(position).getRating());


return convertView;
}
}

}
}


Any help would be greatly appreciated!

Answer Source

You cannot pass a url that is a json string, instead locate the "90" value of the JSONObect for imageUrlsBySize and use the url for the "90" attribute. So change the following line

recipeModel.setImageUrlsBySize(finalObject.getString("imageUrlsBySize"));

To :

String imageUrl = finalObject.getJSONObject("imageUrlsBySize").getString("90")
recipeModel.setImageUrlsBySize(imageUrl);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download