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