How do i parse the deezer api in android - java

i am trying to change my api from last.fm to deezer and it has been a hurdle for me. please help. below is my code and endpoints.
LAST.fm endpoint : http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=c3bbdcbc61af20f159e92612a56c4122&artist=ynw%20melly&track=suicidal&format=json
Deezer endpoint : http://api.deezer.com/search?q=Alan%20Walker%20-%20Darkside
JAVA CODE
static String parsingImageSong(String data) {
if (!TextUtils.isEmpty(data)) {
try {
JSONObject mJsonObject = new JSONObject(data);
if (mJsonObject.opt("track") != null) {
JSONObject mJsTracks = mJsonObject.getJSONObject("track");
if (mJsTracks.opt("album") != null) {
JSONObject mJsAlbum = mJsTracks.getJSONObject("album");
if (mJsAlbum.opt("image") != null) {
JSONArray mJsImgs = mJsAlbum.getJSONArray("image");
if (mJsImgs.length() > 0) {
for (int j = 0; j < mJsImgs.length(); j++) {
JSONObject mJsImg = mJsImgs.getJSONObject(j);
String sizeImg = mJsImg.getString("size");
if (sizeImg.equalsIgnoreCase("extralarge")) {
return mJsImg.getString("#text").replace("300x300","3000x3000");
}
}
}
}
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}

Related

How to get the values of a key of a json file

How can i get the values of a json key in Java here is my code
private void getWebApiData() {
String WebDataUrl = "myjsonfileurl";
new AsyncHttpTask.execute(WebDataUrl);
}
#SuppressLint("StaticFieldLeak")
public class AsyncHttpTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpsURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpsURLConnection) url.openConnection();
if (result != null) {
String response = streamToString(urlConnection.getInputStream());
parseResult(response);
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
if (result != null) {
newsAdapter = new NewsAdapter(getActivity(), newsClassList);
listView.setAdapter(newsAdapter);
Toast.makeText(getContext(), "Data Loaded Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Failed to load data!", Toast.LENGTH_SHORT).show();
}
progressBar.setVisibility(View.GONE);
}
}
private String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Close stream
if (null != stream) {
stream.close();
}
return result;
}
private void parseResult_GetWebData(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("books");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject articleObject = jsonArray.getJSONObject(i);
JSONObject sourceObject = articleObject.getJSONObject("A");
String name = sourceObject.optString("name");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
My json file
{
"books": [
{
"A": [
{
"Amazonite": {
"name": "Amazonite",
"image": "www.google.com"
},
"Amethyst": {
"name": "Amethyst",
"image": "www.google.com"
}
}
],
"B": [
{
"Beryl": {
"name": "Beryl",
"image": "www.google.com"
},
"BloodStone": {
"name": "Bloodstone",
"image": "www.google.com"
}
}
]
}
]
}
What i would like is how to get the values of data under the Alphabet A that is Amazonite and Amethyst and the value of data under Alphabet B but the could i have just give me empty text field nothing no data is being populated.
I have tried with this code but the values retures "null"
private void parseResult_GetWebData(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("books");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject articleObject = jsonArray.getJSONObject(i);
String name = String.valueOf(articleObject.optJSONObject("A"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I am not sure what you really want, but if you just want to get "Amazonite and Amethyst" as you responded under OP, you can try this:
Code snippet
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject articleObject = jsonArray.getJSONObject(i);
JSONArray jsonArrayA = articleObject.getJSONArray("A");
for (int j = 0; j < jsonArrayA.length(); j++) {
JSONObject obj = jsonArrayA.getJSONObject(j);
System.out.println(obj.names());
obj.names().forEach(e -> {
System.out.println(obj.getJSONObject(e.toString()).get("name"));
});
}
}
Console output
["Amazonite","Amethyst"]
Amazonite
Amethyst
Update
I am not sure which name you want to retrieve because both Amazonite and Amethyst appear two times in JSON array A, so I provide two ways to show them as follows:
List<String> names = new ArrayList<>();
List<String> innerNames = new ArrayList<>();
for (int j = 0; j < jsonArrayA.length(); j++) {
JSONObject obj = jsonArrayA.getJSONObject(j);
JSONArray keys = obj.names();
for (int k = 0; k < keys.length(); k++) {
String name = keys.getString(k);
String innerName = obj.getJSONObject(keys.optString(k)).getString("name");
System.out.printf("name: %s, inner name: %s\n", name, innerName);
names.add(name);
innerNames.add(innerName);
}
}
System.out.println(names.toString());
System.out.println(innerNames.toString());
Console output
name: Amazonite, inner name: Amazonite
name: Amethyst, inner name: Amethyst
[Amazonite, Amethyst]
[Amazonite, Amethyst]

Sorting JSONArray using double as comparison violating contract?

I'm trying to sort a JSONArray but I'm getting the following error:
Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:895)
at java.util.TimSort.mergeAt(TimSort.java:512)
at java.util.TimSort.mergeForceCollapse(TimSort.java:453)
at java.util.TimSort.sort(TimSort.java:250)
at java.util.Arrays.sort(Arrays.java:1523)
at java.util.Collections.sort(Collections.java:238)
Here is my code:
private JSONArray SortJSONArray(String json) {
JSONArray sortedJsonArray = new JSONArray();
try {
JSONArray jsonArr = new JSONArray(json);
List<JSONObject> jsonValues = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArr.length(); i++) {
jsonValues.add(jsonArr.getJSONObject(i));
}
Collections.sort(jsonValues, new Comparator<JSONObject>() {
private static final String KEY_NAME = "EUR";
#Override
public int compare(JSONObject a, JSONObject b) {
Double valA = 0.0;
Double valB = 0.0;
try {
valA = a.getDouble(KEY_NAME);
valB = b.getDouble(KEY_NAME);
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
}
if(valA < valB) {
return -1;
} else if( valB < valA) {
return 1;
} else {
return 0;
}
}
});
for (int i = 0; i < jsonArr.length(); i++) {
sortedJsonArray.put(jsonValues.get(i));
}
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
}
return sortedJsonArray;
}
The issue is that either a.getDouble(KEY_NAME) or b.getDouble(KEY_NAME) can throw an exception.
If a.getDouble(KEY_NAME) throws an exception and b.getDouble(KEY_NAME) does not, b.getDouble(KEY_NAME) is never evaluated and both valA and valB remain 0.0.
On the other hand, if you compare a and b is reversed order (i.e. call compare(b,a) instead of compare(a,b), b.getDouble(KEY_NAME) will be evaluated before a.getDouble(KEY_NAME) throws an exception, so valA will be 0.0 and valB will have some other value.
In that case compare(a,b) will return 0 and compare(b,a) will return a non-zero value (assuming b.getDouble(KEY_NAME) doesn't return 0.0), which violates the contract of Comparator.
Evaluating a.getDouble(KEY_NAME) and b.getDouble(KEY_NAME) in separate try blocks will resolve the issue.
try {
valA = a.getDouble(KEY_NAME);
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
}
try {
valB = b.getDouble(KEY_NAME);
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
}
return Double.compare(valA,valB);
Workaround is to return a value in the catch statement:
try {
valA = a.getDouble(KEY_NAME);
valB = b.getDouble(KEY_NAME);
} catch (JSONException e) {
Log.e("MainActivity", e.getMessage());
return -10;
}
if(valA < valB) {
return -1;
} else if( valB < valA) {
return 1;
} else {
return 0;
}

How to retrieve all tagged facebook photos from the facebook sdk

After many attempts I have been unable to retrieve all of my tagged photos from the facebook sdk. I would like something similar to what tinder has, but my query only returns 110 photos, of which some repeat, compared to tinder retrieving all 296 tagged photos.
Here is the code for my request:
Bundle paramaters = new Bundle();
paramaters.putString("fields", "source");
paramaters.putInt("limit", 1000);
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + userId + "/photos",
paramaters,
HttpMethod.GET,
graphCallback
).executeAsync();
GraphRequest.Callback graphCallback = new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
try {
if (response.getError() == null) {
JSONObject joMain = response.getJSONObject();
if (joMain.has("data")) {
final JSONArray jaData = joMain.optJSONArray("data");
for (int i = 0; i < jaData.length(); i++) {
JSONObject joImages = jaData.getJSONObject(i);
String url = (String) joImages.get("source");
if (i == 0) {
facebookAlbum.setDisplayUrl(url);
}
facebookAlbum.addImage(url);
albumnListAdapter.notifyDataSetChanged();
}
}
}
GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
if(nextRequest != null) {
nextRequest.setCallback(this);
nextRequest.executeAsync();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};

Realm Not saving datas

I am trying save the data's which i get from API and my function is as below
try {
// Simulate network access.
mNetworkSubscription = NetworkRequest.performAsyncRequest(api.getPatientsData(tenantID), (data) -> {
// Update UI on main thread
try {
// long pat_id = 0;
if(data != null) {
if(data.getAsJsonObject().get("error") != null){
publishResults("getPatientsData",STATUS_ERROR, null);
}
if (data != null && data.get("result") != null && data.get("result").toString() != "false") {
Realm realm = Realm.getDefaultInstance();
JsonParser parser = new JsonParser();
// realm.beginTransaction();
// realm.where(Patient.class).findAll().deleteAllFromRealm();
//realm.commitTransaction();
try {
JsonArray casesJsonArray = data.get("result").getAsJsonArray();//parser.parse(data.get("result").getAsJsonObject().toString()).getAsJsonArray();
Log.v(Constants.TAG,"PatientJsonArray: "+casesJsonArray);
if(casesJsonArray.size() > 0) {
Patient patientRecord = new Patient();
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
for (int i = 0; i < casesJsonArray.size(); i++) {
try {
JsonObject jsonObject = (JsonObject) casesJsonArray.get(i);
Log.e(Constants.TAG, "execute: jsonObject "+jsonObject);
Log.e(Constants.TAG, "execute: id "+id);
if(realm.where(Patient.class).max("id") != null) {
id = realm.where(Patient.class).max("id").intValue();
Log.e(Constants.TAG, "execute:getPatientsData : In DataSync: " + id);
}
Log.e(Constants.TAG, "execute: jsonObject "+jsonObject);
Log.e(Constants.TAG, "execute: id "+pat_id);
patientRecord.setId(id + 1);
patientRecord.setTenantID(jsonObject.get("tenant_id").getAsLong());
patientRecord.setPatientID(jsonObject.get("patient_id").getAsLong());
patientRecord.setFirstName(jsonObject.get("first_name").getAsString());
patientRecord.setLastName(jsonObject.get("last_name").getAsString());
patientRecord.setPatientWeight(jsonObject.get("patient_weight").getAsString());
patientRecord.setPatientAge(jsonObject.get("patient_age").getAsString());
patientRecord.setGender(jsonObject.get("gender").getAsString());
patientRecord.setStreetAddress(jsonObject.get("street_address").getAsString());
patientRecord.setArea(jsonObject.get("area").getAsString());
patientRecord.setCity(jsonObject.get("city").getAsString());
patientRecord.setZipcode(jsonObject.get("zipcode").getAsString());
patientRecord.setState(jsonObject.get("state").getAsString());
patientRecord.setEmail(jsonObject.get("email").getAsString());
patientRecord.setEnqNumber(jsonObject.get("alternate_number").getAsString());
patientRecord.setEnqName(jsonObject.get("enquirer_name").getAsString());
realm.copyToRealmOrUpdate(patientRecord);
}catch (Exception e){
Log.v(Constants.TAG,"caseList Insert exception: "+e.toString());
}
}
}
});
//realm.close();
}
} catch (Exception e) {
Log.v(Constants.TAG, "getPatientsData Exception: " + e.toString());
}
realm.close();
}
}
} catch (Exception e) {
Log.e(Constants.TAG, "getPatientsData() exception: " + e.toString());
publishResults("getPatientsData",STATUS_ERROR, null);
}finally {
publishResults("getPatientsData",STATUS_FINISHED, null);
}
}, (error) -> {
// Handle Error
Log.e(Constants.TAG,"getPatientsData() Error: "+error.getCause());
publishResults("getPatientsData",STATUS_ERROR, null);
});
}catch (Exception e){
Log.e(Constants.TAG,"getPatientsData() Exception: "+e.toString());
publishResults("getPatientsData",STATUS_ERROR, null);
}
But realm.copyOrUpdate(patientRecords);
is not creating/saving a record in local.
First confirm, if there is no exceptions arises?
Try following workarounds,
Check casesJsonArray.size() > 0 must be true. (OR)
Remove realm.close() and try it once. Sometime it will close realm database connection in between the operation. (OR)
Try with hardcoded(dummy) data.
If above doesn't work then post your Patient Model here.
Note: For auto increment of id you can use following code which will reduce no of database calls-
Number num = realm.where(Patient.class).max("id");
int l_id;
if (num == null) {
i_id = 0;
} else {
i_id = num.intValue() + 1;
}

An Error While parsing data using Jsoup with google app engine

I start using google app engine but I'm a beginner.
I created a web application and I added the library Jsoup.
I'm trying to parse a lot of data from a web site but when I deploy the application I get this error:
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
here is my code :
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("{\"Restaurant\":[");
listPages = new ArrayList<>();
listRestaurant = new ArrayList<>();
listUrlRestaurant = new ArrayList<>();
listObj = new ArrayList<>();
try {
doc = Jsoup.connect(url).userAgent("Mozilla").timeout(60000).get();
//System.out.println(doc.select("strong.next.page-numbers").text());
int i=1;
while(doc.select("strong.next.page-numbers").text().contains("SUIV")){
listPages.add(url);
//System.out.println("exist : "+url);
//System.out.println("*******");
//restaurants = doc.select("div.listing_img > a");
url = url.replace("page/"+i+"/", "page/"+(i+1)+"/");
i=i+1;
//System.out.println("*****"+url);
doc = Jsoup.connect(url).userAgent("Mozilla").timeout(60000).get();
//ParsingRestaurant(restaurants,resp,doc);
}
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(listPages.size());
try{
for (int i = 0; i < listPages.size(); i++) {
doc2 = Jsoup.connect(listPages.get(i)).userAgent("Mozilla").timeout(60000).get();
restaurants = doc2.select("div.listing_img > a");
for (Element element : restaurants) {
listUrlRestaurant.add(element.attr("href"));
//System.out.println(element.attr("href"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(listUrlRestaurant.size());
for (int i = 0; i < listUrlRestaurant.size(); i++) {
ParsingRestaurant(listUrlRestaurant.get(i), resp, doc3,listObj);
}
for (int i = 0; i < listObj.size(); i++) {
if (i!=listObj.size()) {
resp.getWriter().println(listObj.get(i)+",");
}else{
resp.getWriter().println(listObj.get(i));
}
}
resp.getWriter().println("]}");
}
private void ParsingRestaurant(String url, HttpServletResponse resp, Document doc,List<String> listObj) {
// TODO Auto-generated method stub
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Restaurant obj = new Restaurant();
try {
doc = Jsoup.connect(url).userAgent("Mozilla").timeout(60000).get();
name = doc.select("h1.entry-title").first();
obj.setName(name.text());
adress = doc.select("span#frontend_address").first();
obj.setAdress(adress.text());
facebook = doc.select("a#facebook").first();
if (facebook == null) {
obj.setFacebook("empty");
}else{
obj.setFacebook(facebook.attr("href"));
}
phone = doc.select("span.entry-phone.frontend_phone.listing_custom").first();
if (phone == null) {
obj.setPhone("empty");
}else{
obj.setPhone(phone.text());
}
time = doc.select("span.entry-listing_timing.frontend_listing_timing.listing_custom").first();
if (time == null) {
obj.setPhone("empty");
}else{
obj.setTime(time.text());
}
map = doc.select("div.google-map-directory > a ").first();
//System.out.println(name.text()+adress.text()+facebook.attr("href")+phone.text()+time.text());
String location = map.attr("href");
location = location.replace("http://www.google.com/maps/dir/Current+Location/", "");
String[] output = location.split(",");
obj.setLongitude(output[0]);
obj.setLatitude(output[1]);
images = doc.select("a.listing_img.galerie_listing");
for (Element e : images) {
obj.images.add(e.attr("href"));
}
details = doc.select("div#listing_apercu > div");
for (Element e : details) {
//System.out.println(e.select("label").text());
obj.titles.add(e.select("label").text());
String x = e.select("p > span").text();
for (int j = 1; j < x.length(); j++) {
if (Character.isUpperCase(x.charAt(j))) {
x = changeCharInPosition(j-1, ',', x);
}
}
obj.details.add(x);
}
String json = gson.toJson(obj);
listObj.add(json);
} catch (IOException e) {
e.printStackTrace();
}
}
public String changeCharInPosition(int position, char ch, String str){
char[] charArray = str.toCharArray();
charArray[position] = ch;
return new String(charArray);
}
}
any idea about the problem?!

Categories