我需要在服务器上拍摄的recyclerview
张照片中显示。
从server
正确提取图片,但glide
没有向我显示图片,我不知道原因。
我告诉你我的代码:
VIDEO:
public class Video {
public String pic;
public String sottotitolo;
public String titolo;
public String data;
public Video() {
}
public Video(String pic,String sottotitolo,String titolo,String data){
this.pic = pic;
this.sottotitolo=sottotitolo;
this.titolo=titolo;
this.data=data;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getSottotitolo() {
return sottotitolo;
}
public void setSottotitolo(String sottotitolo) {
this.sottotitolo = sottotitolo;
}
public String getTitolo() {
return titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
ADAPTER:
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoHolder> {
private ArrayList<Video> mData;
private ArrayList<Video> mData2;
private Activity mACtivity;
public VideoAdapter(ArrayList<Video> data,ArrayList<Video> data2,Activity activity) {
this.mData = data;
this.mData2 = data2;
this.mACtivity = activity;
}
@Override
public VideoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.anteprima_list_item, parent, false);
return new VideoHolder(view);
}
@Override
public void onBindViewHolder(VideoHolder holder, int position) {
Video video = mData.get(position);
Video video2 = mData2.get(position);
holder.setTitolo(video.getTitolo());
holder.setSottoTitolo(video.getSottotitolo());
holder.setData(video.getData());
holder.setData(video.getData());
/* holder.setAddress(restaurant.getAddress());
holder.setCost("Average cost for 2: " + restaurant.getCurrency() + restaurant.getCost());
holder.setRating(restaurant.getRating());*/
Glide.with(mACtivity)
.load(video2.getPic())
.into(holder.restaurantImageView);
}
@Override
public int getItemCount() {
if (mData == null)
return 0;
return mData.size();
}
public class VideoHolder extends RecyclerView.ViewHolder {
ImageView restaurantImageView;
TextView restaurantNameTextView;
TextView restaurantAddressTextView;
TextView restaurantRatingTextView;
TextView costTextView;
TextView distanceTextView;
public VideoHolder(View itemView) {
super(itemView);
restaurantImageView = (ImageView) itemView.findViewById(R.id.imageview_restaurant);
restaurantNameTextView = (TextView) itemView.findViewById(R.id.textview_restaurant_name);
restaurantAddressTextView = (TextView) itemView.findViewById(R.id.restaurant_address_textview);
distanceTextView = (TextView) itemView.findViewById(R.id.restaurant_distance_textview);
/* costTextView = (TextView) itemView.findViewById(R.id.cost_for_two_textview);
restaurantRatingTextView = (TextView) itemView.findViewById(R.id.rating);*/
}
public void setTitolo(String titolo) {
restaurantNameTextView.setText(titolo);
}
public void setSottoTitolo(String sottotitolo) {
restaurantAddressTextView.setText(sottotitolo);
}
public void setData(String data) {
distanceTextView.setText(data);
}
/* public void setPic(String pic) {
distanceTextView.setText(pic);
}
public void setCost(String cost) {
costTextView.setText(cost);
}
public void setDistance(String distance) {
distanceTextView.setText(distance);
}*/
}
}
主要活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView videoRecyclerView;
private VideoAdapter mAdapter;
private String TAG = MainActivity.class.getSimpleName();
//private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "https://myapi.com";
ArrayList<Video> videoList;
ArrayList<Video> videoList2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoList = new ArrayList<>();
videoList2 = new ArrayList<>();
mAdapter = new VideoAdapter(videoList,videoList2, this);
videoRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
videoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
videoRecyclerView.setHasFixedSize(true);
videoRecyclerView.setAdapter(mAdapter);
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
/* pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();*/
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
}
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONObject("data").getJSONArray("output");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
Video video=new Video();
String titolo = c.getString("titolo");
String sottotitolo = c.getString("sottotitolo");
String data = c.getString("date");
video.setTitolo(titolo);
video.setSottotitolo(sottotitolo);
video.setData(data);
videoList.add(video);
}
JSONArray img_url = jsonObj.getJSONObject("data").getJSONArray("output2");
// looping through All Contacts
for(int i=1;i<=11;i++)
{
String c = img_url.getString(i);
// Log.i("Value is:::",""+c);
System.out.println("C:"+c);
Video video=new Video();
videoList2.add(video);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mAdapter.notifyDataSetChanged();
// Dismiss the progress dialog
/* if (pDialog.isShowing())
pDialog.dismiss();*/
/**
* Updating parsed JSON data into ListView
* */
/* ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setAdapter(adapter);*/
}
}
}
BUILD GRADLE:
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.marco.ud2"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'org.apache.httpcomponents:httpclient:4.5.3'
implementation 'org.jsoup:jsoup:1.11.1'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.github.bumptech.glide:glide:3.8.0'
compile 'com.android.volley:volley:1.0.0'
}