How to change URL from JSON parsing - android

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");

Related

Not able to fetch JSON data [using volley Library] in Android

The jason array request code goes like this:
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET,url, (JSONArray) null , new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
Log.d("Response:", String.valueOf(response.getJSONObject(0)));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Response not recieved", Toast.LENGTH_SHORT).show();
}
});
And I've used a singleton instance of a request queue, to fetch the data,
AppController.getInstance(context.getApplicationContext()).addToRequestQueue(arrayRequest);
I'm using an api service that supplies a bunch of questions (The api generates a url, which returns a collection of JSON Objects, An Array (Just try and Open the url link, the json array will be seen)
But when I run the app, the request is not even getting a response,the progam flow is going into the error listner block.
Can someone explain this behaviour? Is it because the JSON array supplied by the url, has nested arrays? Why?
I tried reading and anlyzing other questions here, which might have the same issue, But i found nothing.
You want to just change JsonArrayRequest to JsonObjectRequest:
Please copy and paste below code:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String category = jsonObject.getString("category");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
// hide the progress dialog
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq, "TAG");
Follow this link for learn other request: Androidhive

Volley Get Request: onResponse is never called

im pretty new to Android Studio and I'm trying to build a Get Request using Volley, the Server response is a JsonObject. I tested the code with breakpoints but I wonder why I don't jump into onResponse or why it won't work.
Here's my Code of the Get Method:
public Account GetJsonObject(String urlExtension, String name, Context context) {
String baseURL = "myurl.com/api";
baseURL += urlExtension + "/" + name;
// url will look like this: myurl.com/api/user/"username"
final Account user = new Account("","");
//Account(name, email)
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
}
catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObject);
return user;
}
As #GVillavani82 commented your onErrorResponse() method body is empty. Try to log the error like this
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
}
Make sure that you have the below permission set in AndroidManifest.xml file and the api URL is proper.
<uses-permission android:name="android.permission.INTERNET"/>
And JsonObjectRequest class returns Asynchronous network call class. Modify your code like below.
// remove Account return type and use void
public void GetJsonObject(String urlExtension, String name, Context context) {
....
.... // other stuffs
....
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.GET, baseURL,null,
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
#Override
public void onResponse(JSONObject response) {
processResponse(response); // call to method
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("ERROR", "Error occurred ", error);
}
});
requestQueue.add(jsonObject);
}
Now create another method like below
private void processResponse(JSONObject response) {
try {
final Account user = new Account("","");
JSONObject obj = response.getJSONObject("userObject");
String username = obj.getString("Username");
String email = obj.getString("Email");
user.setUsername(username);
user.setEmail(email);
} catch (JSONException e) {
e.printStackTrace();
}
}

Android App Crashing While Trying To Parse Json Object Using Volley

requestQueue = Volley.newRequestQueue(Home.this);
JsonObjectRequest objectRequest = new JsonObjectRequest(com.android.volley.Request.Method.GET ,"http://www.omdbapi.com/?t=avengers",null,new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String Title = response.getString("Title");
String Year = response.getString("Year");
String Released = response.getString("Released");
String Genre = response.getString("Genre");
String Metascore = response.getString("Metascore");
String imdbRating = response.getString("imdbRating");
String Poster = response.getString("Poster");
moviesData = new String[]{Title, Year, Released, Genre, Metascore, imdbRating, Poster};
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(objectRequest);
I am trying to fetch data from the site and store its content using json parsing and volley i was doing it with HttpUrlConnection and it was working fine when i tried to do it using volley I got stuck
I don't know where I am going wrong or missing something don't know why App is crashing.
That was my own fault I was writing this code in the wrong method.

Android JSON VOlley

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();
}

Android Volley: Combine JsonObjectRequest & ImageLoader, NullPointerException

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.

Categories

Resources