0

In this link: https://uhunt.onlinejudge.org/api/uname2uid/felix_halim this api is returning just a String!

like this:

339

I tried this way but this is not working:

String link = "https://uhunt.onlinejudge.org/api/uname2uid/felix_halim"; // getting userID
        Log.d("tttt", link);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, link, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    String inline = response.toString();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Log.d("tttt", "error");
                }
        });

One more thing I'm using this on Android studio 3.1 and using library "'com.android.volley:volley:1.1.0'"

Please help me! Any kind of help will be appreciated. Thanks.

Ivan Smetanin
  • 1,999
  • 2
  • 21
  • 28
Farid Chowdhury
  • 2,766
  • 1
  • 26
  • 21

2 Answers2

0

Quickly explored the API, and it looks like the endpoint you are accessing has only the string "339" in it (Open the link in your browser).

Sending a GET request to this endpoint through Postman REST Client also gives 339, showing that this is indeed a backend side issue.

Other endpoints have their responses in JSON format. Try another endpoint like: https://uhunt.onlinejudge.org/api/contests/id/10 and you will see you have a valid JSONObject.

Fix the issue on the backend to display data in JSON for this endpoint and your response on the frontend should reflect the same.

Special K
  • 55
  • 10
  • 1
    He's asked what to change in his android project so that he can parse the string. This answer doesn't answer the question. – Debanjan May 27 '18 at 11:12
0

I don't you whether your service endpoint is correct or not but it does return 339. It clearly means your service api is not returning a json string. Your first priority should be to get correct endpoint. Once you have the correct endpoint please follow below mentioned steps to get Java objects from json string.

For example, you have following json string that you want to convert into Java object:

{"firstname":"Ashwani","lastname":"Kumar","age":"30"}

Add Jackson parser dependencies to be included in the gradle file.

compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-core:2.8.5'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'

Exclude META-INF/LICENSE in the android tag in the gradle file to avoid dup license file error in Android studio

packagingOptions {
    exclude 'META-INF/LICENSE'
}

Create a model for your json string parsing. use the following service to autogenerate models: http://www.jsonschema2pojo.org/

Paste your json string into the and select JSON in source type. click preview or download button to get generated java classes. In this case we get following java class:

package com.example;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstname",
"lastname",
"age"
})
public class Person {

@JsonProperty("firstname")
private String firstname;
@JsonProperty("lastname")
private String lastname;
@JsonProperty("age")
private String age;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("firstname")
public String getFirstname() {
return firstname;
}

@JsonProperty("firstname")
public void setFirstname(String firstname) {
this.firstname = firstname;
}

@JsonProperty("lastname")
public String getLastname() {
return lastname;
}

@JsonProperty("lastname")
public void setLastname(String lastname) {
this.lastname = lastname;
}

@JsonProperty("age")
public String getAge() {
return age;
}

@JsonProperty("age")
public void setAge(String age) {
this.age = age;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

Parse json string to java object using following code:

//Example Json String
String personJsonStr = "{\"firstname\":\"Ashwani\",\"lastname\":\"Kumar\",\"age\":\"30\"}";
//Pass the jason string and model class you want to convert you json string into
Person person = mapper.readValue(personJsonStr, Person.class);                               
// read from json string
String firstName = person.getFirstname();
String lastName = person.getLastname();
String age = person.getAge();
Ashwani Kumar
  • 834
  • 3
  • 16
  • 30