我能够使用Volley在textview上加载JSON数据,但是Picasso处理了图像以将其加载到ImageView
。这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
Context c = getActivity().getApplicationContext();
tvQuote = (TextView) v.findViewById(R.id.quote);
tvAuthor = (TextView) v.findViewById(R.id.author);
Typeface MP = Typeface.createFromAsset(getActivity().getAssets(), "font/MP.ttf");
tvQuote.setTypeface(MP);
tvAuthor.setTypeface(MP);
imageFromURL=(ImageView)v. findViewById(R.id.imgUrl);
getQuote();
loadImageFromURL();
//Picasso.with(getActivity().getApplicationContext()).load(jsonImage).into(imageFromURL);
Log.d("Picasso","Error");
return v;
//return inflater.inflate(R.layout.fragment_home, container, false);
}
这是我使用Volley从URL加载图片的方法
private void loadImageFromURL() {
List<Integer> imageList = new ArrayList<Integer>();
imageList.add(R.drawable.ic_action_loader);
//swipeRefreshLayout.setRefreshing(true);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
imgURL, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject urls = response.getJSONObject("urls");
String regular = urls.getString("regular");
JSONObject user = response.getJSONObject("user");
String name = user.getString("name");
String username = user.getString("username");
jsonImage = regular;
jsonName = name;
jsonUserName = username;
Picasso.with(getActivity().getApplicationContext())
.load(jsonImage)
//.placeholder(R.drawable.desert)
.into(imageFromURL);
Log.d(TAG, jsonImage);
tvPhotographer.setClickable(true);
tvPhotographer.setMovementMethod(LinkMovementMethod.getInstance());
String authorLink = "Photo By - <a href='https://unsplash.com/@" + jsonUserName + "'>" + jsonName + "</a> / <a href='https://unsplash.com/'>Unsplash</a>";
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(authorLink, Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(authorLink);
}
tvPhotographer.setText(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
jsonObjReq.setTag(TAG);
if (mInstance != null) {
// Add a request to your RequestQueue.
mInstance.addToRequestQueue(jsonObjReq);
// Start the queue
mInstance.getRequestQueue().start();
}
}
从stringrequest
加载的文本在Volley的文本视图中完美加载,但只加载了图像。
我在主要活动上尝试了这种方法,但它确实有效。 但为什么它不会加载到片段部分?
答案 0 :(得分:0)
这样做
Picasso.with(getContext())
.load(jsonImage)
.into(imageFromURL);