Java 使用JSON文件中解析信息的URL在URL中显示图像
Im使用截击的GET请求: HTTPRequest.javaJava 使用JSON文件中解析信息的URL在URL中显示图像,java,android,json,parsing,android-volley,Java,Android,Json,Parsing,Android Volley,Im使用截击的GET请求: HTTPRequest.java public void get(String url) { // Request a string response from the provided URL. JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, getReponse(), createMyReqErrorListener())
public void get(String url) {
// Request a string response from the provided URL.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url,
getReponse(), createMyReqErrorListener()) {
@Override
protected void deliverResponse(JSONObject response) {
Log.d(TAG, "deliverResponse= " + response.toString());
super.deliverResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put(ACCEPT, ACCEPT_JSON);
headers.put(AUTH_ID, AUTH_ID_VALUE);
headers.put(PLATFORM, PLATFORM_VALUE);
headers.put(CID, "");
System.out.println(headers.toString());
return headers;
}
};
// Add the request to the RequestQueue.
queue.add(request);
}
public String parseResponse(JSONObject response) {
try {
JSONObject objectBlockPromo = response.getJSONObject("BlockPromotions");
JSONObject objectItems = objectBlockPromo.getJSONObject("Items");
JSONArray arrayItem = objectItems.getJSONArray("Item");
for (int i = 0; i < arrayItem.length(); i++) {
JSONObject item = arrayItem.getJSONObject(i);
ImageURL = item.getString("DisplayImage");
System.out.println(ImageURL);
}
} catch (JSONException e) {
e.printStackTrace();
}
return ImageURL;
}
我用来解析JSON的代码如下:
Parser.java
public void get(String url) {
// Request a string response from the provided URL.
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url,
getReponse(), createMyReqErrorListener()) {
@Override
protected void deliverResponse(JSONObject response) {
Log.d(TAG, "deliverResponse= " + response.toString());
super.deliverResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put(ACCEPT, ACCEPT_JSON);
headers.put(AUTH_ID, AUTH_ID_VALUE);
headers.put(PLATFORM, PLATFORM_VALUE);
headers.put(CID, "");
System.out.println(headers.toString());
return headers;
}
};
// Add the request to the RequestQueue.
queue.add(request);
}
public String parseResponse(JSONObject response) {
try {
JSONObject objectBlockPromo = response.getJSONObject("BlockPromotions");
JSONObject objectItems = objectBlockPromo.getJSONObject("Items");
JSONArray arrayItem = objectItems.getJSONArray("Item");
for (int i = 0; i < arrayItem.length(); i++) {
JSONObject item = arrayItem.getJSONObject(i);
ImageURL = item.getString("DisplayImage");
System.out.println(ImageURL);
}
} catch (JSONException e) {
e.printStackTrace();
}
return ImageURL;
}
公共字符串解析响应(JSONObject响应){
试一试{
JSONObject objectBlockPromo=response.getJSONObject(“BlockPromotions”);
JSONObject objectItems=objectBlockPromo.getJSONObject(“项”);
JSONArray arrayItem=objectItems.getJSONArray(“项”);
对于(int i=0;i
我设法单独获取了所有的url,我的问题是将这些url返回到主屏幕,以显示图像
我该怎么做
我尝试过将URL用作字符串,也尝试过(但我认为我做得不正确)使用ImageRequestfromVolley获取URL
谢谢您需要打开到URL的连接,下载图像,然后显示它
String name = "yourdesiredfilename";
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
OutputStream output = openFileOutput(name, MODE_PRIVATE);
IOUtils.copy(input, output);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(output);
(这只是因为我懒惰;)然后,您可以在本地获取该文件,并将其传递给ImageView或任何您拥有的文件
Drawable d = Drawable.createFromPath(pathName);
ImageView yourView;
yourView.setImageDrawable(d);
在某些情况下,您需要缓存下载的照片,因此我建议您尝试使用一些最常用的ImageLoader库 最受欢迎,由谷歌推荐:或 在将其中一些UTIL导入到项目中之后,在某些情况下,您只需调用一个函数,如mImageLoader(mphotour)并将其附加到ImageView或其他任何内容,最终达到问题的目标;-)
我知道这不是一个非常具体的答案,但我希望在我为同样的事情苦苦挣扎时有人告诉我这一点。我觉得你问题的标题有误导性。您可以从json文档中检索、检索和使用数据。现在你想显示的图像,你知道的网址。好的一点,我会改变它!谢谢我已经在用截击来拉下JSON了。在我看来,我想我可以使用JSON中提供的URL从Volley下载ImageRequest?我确信有一个比必须做两个这样的请求更好的方法?好吧,在某个时候,你必须下载图像并从本地副本显示它。无论您是通过url连接实现这一点,像glide或Volley这样的库都是您个人的喜好。