Mein Code zeigt einen Fehler und ich weiß nicht, was es tatsächlich verursacht, diese Zeile: HttpURLConnection con = client.open (neue URL (imageUrl));OkHttpClient() offene Methode zeigt Fehler
package www.com.easyrecepee;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
* Created by Joshua on 9/28/2016.
*/
public class PlacesListAdapter extends ArrayAdapter<Places> {
private Context context;
private List<Places> placesList;
private LruCache<String, Bitmap> imageCache;
// create a field of the RequestQueue to be used by volley
// so it persist through out the life time of the class
//private RequestQueue queue;
public OkHttpClient client = new OkHttpClient();
public PlacesListAdapter(Context context, int resources, List<Places> objects) {
super(context, resources, objects);
this.context = context;
this.placesList = objects;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
final int cacheSize = maxMemory/8;
imageCache = new LruCache<>(cacheSize);
// instantiate the queue field and passing current context
// so its associated with proper activity
//queue = Volley.newRequestQueue(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_vicinity, parent, false);
// Display places name in the TextView widget
final Places places = placesList.get(position);
TextView placeTitle = (TextView) view.findViewById(R.id.place_title);
TextView vicinity = (TextView) view.findViewById(R.id.vicinity);
//TextView openNow = (TextView)view.findViewById(R.id.openNow);
placeTitle.setText(places.getName());
vicinity.setText(places.getVicinity());
// if(places.isOpenNow()){
// openNow.setText("Open");
// } else {
// openNow.setText("Closed");
// }
//Display place photo in ImageView widget
Bitmap bitmap = imageCache.get(places.getId());
// for volley
final ImageView image = (ImageView) view.findViewById(R.id.place_image);
if (bitmap != null) {
//For the volley, this commented line of code is refactored so it can
// be called outside this block
//ImageView image = (ImageView) view.findViewById(R.id.place_image);
image.setImageBitmap(places.getBitmap());
} else {
// Retrieves image url
//String imageUrl = places.getIconUrl();
// declare instance of image request
/*ImageRequest request = new ImageRequest(imageUrl,
new Response.Listener<Bitmap>(){
@Override
public void onResponse(Bitmap arg0) {
image.setImageBitmap(arg0);
imageCache.put(places.getId(), arg0);
}
},
80, 80,
Bitmap.Config.ARGB_8888,
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError arg0) {
Log.d("PlacesAdapter", arg0.getMessage());
}
}
);
//adding request to queue
queue.add(request);*/
/*
The line of code below is used by the ImageLoader AsyncTask
Uncomment if using AsyncTask
*/
PlaceAndView container = new PlaceAndView();
container.places = places;
container.view = view;
ImageLoader loader = new ImageLoader();
loader.execute(container);
}
return view;
}
// Used with AsyncTask, since am using Volley, no need
class PlaceAndView {
public Places places;
public View view;
public Bitmap bitmap;
}
// Using the Volley Method does what the AsyncTask do
// Uncomment to use
private class ImageLoader extends AsyncTask<PlaceAndView, Void, PlaceAndView> {
@Override
protected PlaceAndView doInBackground(PlaceAndView... params) {
PlaceAndView container = params[0];
Places places = container.places;
try {
String imageUrl = places.getIconUrl();
// Using OkHttpClient to fetch images
HttpURLConnection con = client.open(new URL(imageUrl));
//HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = con.getInputStream();
}
//InputStream in = (InputStream) new URL(imageUrl).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
places.setBitmap(bitmap);
in.close();
container.bitmap = bitmap;
return container;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(PlaceAndView result) {
ImageView image = (ImageView) result.view.findViewById(R.id.place_image);
image.setImageBitmap(result.bitmap);
// saves image for future use
//result.places.setBitmap(result.bitmap);
imageCache.put(result.places.getId(), result.bitmap);
}
}
}
Wie kann ich die offene machen() Methode stoppt Fehler zeigte, und das ist die einzige Methode, Fehler im Code zu schaffen, so dass ich das Gefühl, es sollte eine andere Methode oder eine neuere Art und Weise die offene Methode zu behandeln.
Und was ist der Fehler genau? – Michael
Sie sagen es ist "Fehler anzeigen". Meinst du, es gibt einen Fehler, wenn der Code ausgeführt wird (Laufzeitfehler), ein Fehler in der IDE (Kompilierzeitfehler) oder etwas anderes? –
Die Klasse 'OkHttpClient' hat keine Methode' open (...) '. https://github.com/square/okhttp/blob/6e236ce3b80f21369dc544f0e1053ff71be8689b/okhttp/src/main/java/com/squarepup/okhttp/OkHttpClient.java – betorcs