【发布时间】:2017-08-02 12:26:56
【问题描述】:
我能够使用 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 的 textviews 上完美加载,但只有图像没有加载。
我已经在主要活动上尝试过这种方法并且它有效。 但是为什么片段部分没有加载呢?
【问题讨论】:
-
你能在
regular中获取图片加载url吗?你在jsonImage得到了什么? -
我在 jsonImage 中获取图像 url...当控件到达 picasso 时应用程序崩溃
标签: java android android-fragments android-volley picasso