I encounter a situation that first the image's url should be get from weibo's API, and then, I wanna show the image based on the URL:
The Volley Lib is used here, and Here is my code:
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i("Result", response.toString());
try {
String weiboNickName = response.getString("name");
weiboNameMain.setText(weiboNickName);
weiboUrl = response.getString("avatar_hd");
weiboUrl = weiboUrl.replace("\\", "");
Toast.makeText(MainPage.this, "url: " + weiboUrl, Toast.LENGTH_SHORT).show(); imageLoader = new ImageLoader(queue, new BitmapLruCache(BitmapLruCache.getDefaultLruCacheSize()));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsObjRequest);
imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));
However, I will get a NullPointerException when use a weiboUrl for the last sentense:
imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));
I know this is because that Volley will create another Thread, so I should not use the reference in the main thread before Thread in Request ended.
Is there some ideas that could use to overcome this problem?
Thanks in advance.
Best
---Updated
The Problem is that I put
imageLoader.get(weiboUrl,ImageLoader.getImageListener(weiboAvatarMain,R.drawable.user, R.drawable.user));
in a wrong position. If I put it within the jsObject's onResponse() method, the problem will be gone. Then the image cache could be used. (ImageRequest does not own cache mechanism itself)
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.i("Result", response.toString());
try {
String weiboNickName = response.getString("name");
weiboNameMain.setText(weiboNickName);
weiboUrl = response.getString("avatar_hd");
weiboUrl = weiboUrl.replace("\\", "");
Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
#Override
public void onResponse(Bitmap bitmap) {
String weiboAvatar = ImageHandler.storeImage(bitmap, getRoot(), getWeiboAvatarName());
Log.i("store info", weiboAvatar);
Editor edit = preference.edit();
edit.putString("weiboAvatar", weiboAvatar);
edit.apply();
weiboAvatarMain.setImageBitmap(bitmap);
}
};
ImageRequest imgRequest = new ImageRequest(weiboUrl, listener, 0, 0, null, null);
queue.add(imgRequest);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsObjRequest);
I use the ImageRequest instead of ImageLoader to overcome this, it works. The scenario here is the image will not change overtime, so I then save it into the local file.
However, as ImageLoader support cache for image reading, I really need the solutions for cache reading.
Thanks.
Related
hi guys i am making an Android app which enlarge the Instagram profile pictures
i have done everything correctly now what i want is to modifiy the output url that i am getting from instagram server for example when i run my json script it give me this
https://instagram.fkhi6-1.fna.fbcdn.net/t51.2885-19/s320x320/20766978_110444579680760_4754914132547862528_a.jpg
and i want to convert this to that
https://instagram.fkhi6-1.fna.fbcdn.net/t51.2885-19/s800x800/20766978_110444579680760_4754914132547862528_a.jpg
here is my code that i am using
final JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, finalURL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject user = response.getJSONObject("user");
String profilePicture = user.getString("profile_pic_url_hd");
Log.v("JSON", "User: " + profilePicture);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.v("FUN", "Error " + error.toString());
}
});
Thanks.
Try this:
String imageUrl=originalUrl.replaceAll("[s][0-9]+[x][0-9]+","s"+desiredWidth+"x"+desiredHeight);
Try this code and see if it works:
String smallImageUrl = "https://instagram.fkhi6-1.fna.fbcdn.net/t51.2885-19/s320x320/20766978_110444579680760_4754914132547862528_a.jpg";
String largeImageURL = smallImageUrl.replace("s320x320", "s800x800");
I'm trying to set up volley to pull down this JSONObject from iTunes
String url = "https://itunes.apple.com/search?term=michael+jackson";
The whole point is to parse the object to get the album art url for a particular song
So I set this code up to get the JsonObject, and I'm getting a couple of errors
Cannot resolve symbol ErrorListener
Cannot resolve symbol Listener
Here is the code with annotated errors
JsonObjectRequest jsonRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Downloader.Response.Listener // Cannot resolve symbol Listener
<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// the response is already constructed as a JSONObject!
try {
response = response.getJSONObject("args");
String site = response.getString("site"),
network = response.getString("network");
System.out.println("Site: "+site+"\nNetwork: "+network);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Downloader.Response.ErrorListener // Cannot resolve symbol ErrorListener
() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(this).add(jsonRequest);
Try Response.ErrorListener
http://afzaln.com/volley/com/android/volley/Response.ErrorListener.html
There is no Downloader.ResposneListener
Change TO
StringRequest stringRequest = new StringRequest(DownloadManager.Request.Method.GET, url,
to
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
Your code will be
JsonObjectRequest jsonRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new com.android.volley.Response.Listener // CHANGES HERE
<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// the response is already constructed as a JSONObject!
try {
response = response.getJSONObject("args");
String site = response.getString("site"),
network = response.getString("network");
System.out.println("Site: "+site+"\nNetwork: "+network);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new com.android.volley.Response.ErrorListener // CHANGES HERE
() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(this).add(jsonRequest);
Good Day, I would like to get data from this url, and I have problems with onResponse method implementation:
I have something like this:
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://horoscope-api.herokuapp.com/",
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
//The Problem is here
JSONArray jsonArray = response.getJSONArray("");
String author = jsonArray.getString(0);
textView.setText(author);
}catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(jsonObjectRequest);
}
I don`t understand how to implement onResponse method correctly, I know that I must to get string from array or object, but data from link make me problems to understand what is it object or array, can you write me how to get author field from this url.
Thank you
Since the response in your link is JSONObject, you only need to do the following inside onResponse:
String author = response.getString("author");
textView.setText(author);
Hope this helps!
The response that you are getting is not a JSONArray but is a JSONObject. You could try the below code and see if you are able to get the author.
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://horoscope-api.herokuapp.com/",
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String author = response.get("author");
textView.setText(author);
}catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(jsonObjectRequest);
}
Hope this helps.
The data from link is JSONObject not JSONArray. Call response.getString("author"); to get author string.
try{
String author = response.getString("author");
} catch(JSONException e) {
e.printStackTrace();
}
I am familiar with Volley and creating a Singleton class, and adding requests to the queue. However, I wish to increase the modularity of volley and simply call all requests through method call to another class instead. I have setup the present action as basis for the common GET request:
public Object getRequest(String params) {
final JSONObject getRequestReturn = new JSONObject();
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
VolleySingleton.prefixURL, ((String) null),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
getRequestReturn = response;
Log.v("GET Request value", response.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("GET Request Error", error.toString());
}
});
mRequestQueue.add(getRequest);
return getRequestReturn;
}
However, I have a the perplexing catch 22 error on the assignment of response at:
getRequestReturn = response;
The error notes that getRequestReturn must be declared final to allow for use within the inner class, but upon assigning final, another error appears noting that you cannot assign a value to a final variable.
How can this method be handled?
Declare JSONObject as global and initialize in same place like this.
JSONObject getRequestReturn ;
getRequestReturn = new JSONObject();
public Object getRequest(String params) {
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
VolleySingleton.prefixURL, ((String) null),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
Log.v("GET Request value", response.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("GET Request Error", error.toString());
}
});
mRequestQueue.add(getRequest);
return new JSONObject();
}
U can't get response when u return!Volley request is async!
I use EventBus and I do it like this :
When I need to get data from web , i add a request like this.
Then when i get response from web ,I use EventBus to post a event.
I get the event and update my page.
Or U can try the RxAndroid.
I am using volley library to get data from an api in JSON format, and it works like a charm. I ask for JSON file, i get a response, i parse that respons and populte the views in activity.
Now, i have a button to share the data using Intetnt.ACTION_SEND and putExtra(Intent.EXTRA_TEXT) that takes string as the 2nd parameter.
The problem is, i make a string variable and append data in that string variable inside volley on response method. but as volley makes a new thread for getting data from api the string is not updated and Intetnt.EXTRA_TEXT sends an empty string.
I want to ask, if there is anything similer to onPostExecute method for volley? where i can set the data to some variables after the thread is done processing. or any alternate methods to do the same.
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSearchString, null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
JSONArray bookArray = response
.getJSONArray("items");
JSONObject bookObject = bookArray.getJSONObject(0);
try {
bookSelfLink = bookObject.getString("selfLink");
JsonObjectRequest newJsObjRequest = new JsonObjectRequest(
Request.Method.GET, bookSelfLink, null,
new Response.Listener<JSONObject>() {
public void onResponse(
JSONObject response) {
try {
JSONObject newBookObject = response
.getJSONObject("volumeInfo");
try {
dBookPages.setText("Pages - "
+ newBookObject
.getString("pageCount"));
eMailText = eMailText + newBookObject
.getString("pageCount"));
} catch (JSONException jse) {
dBookPages
.setText("Pages not found");
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(
VolleyError error) {
// TODO Auto-generated method
// stub
}
});
AppController.getInstance().addToRequestQueue(
newJsObjRequest, tag_json_obj);
} catch (JSONException jse) {
jse.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
AppController.getInstance().addToRequestQueue(jsObjRequest,
tag_json_obj);
from the Volley v1.0.0 Documentation:
public boolean hasHadResponseDelivered()
Returns true if this request has had a response delivered for it.
I hope that this answer would help someone with the same question.
There were 2 work around for the record.
1) make network call on button click -> wait for response -> fire intent inside onResponse with bundled data.
2) pass url as string in bundle -> call intent for new activity -> make network request pars data.