I am trying to parse JSON from a URL to then add data to an array.
I am using the GSON library.
My JSON has the following format:
[
{
"img-src":"http://website.com/images/img1.png",
"URL":"http://google.com"
},
{
"img-src":"http://website.com/images/img2.jpg",
"URL":"http://yahoo.com"
}
]
I want to grab the above data in a separate thread, I have the following code:
public class Async extends AsyncTask<String, Integer, Object>{
#Override
protected String doInBackground(String... params) {
return null;
}
}
How do I go about grabbing each "img-src" and "URL" values?
Use this method to fetch your Data in an Array list
public ArrayList<NewsItem> getNews(String url) {
ArrayList<NewsItem> data = new ArrayList<NewsItem>();
java.lang.reflect.Type arrayListType = new TypeToken<ArrayList<NewsItem>>(){}.getType();
gson = new Gson();
httpClient = WebServiceUtils.getHttpClient();
try {
HttpResponse response = httpClient.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
Reader reader = new InputStreamReader(entity.getContent());
data = gson.fromJson(reader, arrayListType);
} catch (Exception e) {
Log.i("json array","While getting server response server generate error. ");
}
return data;
}
This should be how you should declare your ArrayList Type class (here its NewsItem)
import com.google.gson.annotations.SerializedName;
public class NewsItem {
#SerializedName("title")
public String title;
#SerializedName("content")
public String title_details;
#SerializedName("date")
public String date;
#SerializedName("featured")
public String imgRawUrl;
}
Here is the WebSErvice Util Class.
public class WebServiceUtils {
public static HttpClient getHttpClient(){
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 50000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 50000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
return httpclient;
}
}
That's the code I use (working well for me).
//Initialize the list
Type listType = new TypeToken<ArrayList<YourObject>>(){}.getType();
//Parse
List<YourObject> List= new Gson().fromJson(response, listType);
YourObject should be something like :
public class Category {
private String URL;
private String img-src;
public Category(String URL, String img-src){
this.URL= URL;
this.img-src= img-src;
}
}
Regards.
Ps: With this you will obtain a list of "YourObject". Then you can create to list one with the URL and other with img-src
In this user guide you can find a lot of examples:
https://sites.google.com/site/gson/gson-user-guide
Related
I've a response class
public class ResponseModel<T> {
private boolean isRequestSuccessful;
public boolean getIsRequestSuccessful() {
return this.isRequestSuccessful;
}
public void setIsRequestSuccessful(boolean isRequestSuccessful) {
this.isRequestSuccessful = isRequestSuccessful;
}
private String message;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
private T object;
public T getObject() {
return this.object;
}
public void setObject(T object) {
this.object = object;
}
}
My API will return type T. I would like to parse the response from the API and create a object of type ResponseModel.
I am trying to achieve something like below which I can do it easily with c#. Please help on how to do this with Java
public static ResponseModel<T> Get(String requestUri) throws ClientProtocolException,IOException {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(requestUri);
httpGet.addHeader("TenantKey", TenantKey);
httpGet.addHeader("accept", "application/json");
HttpResponse response = client.execute(httpGet);
ResponseModel<T> responseModel = new ResponseModel<T>();
if (response.getStatusLine().getStatusCode() == 200) {
Gson gson = new GsonBuilder().create();
// parse the response as T and and assign to object of ResponseModel
responseModel.object = ...
}
else
{
responseModel.message = response.getEntity().getContent();
}
// return ResponseModel here
}
Generics in C# and Java are pretty different. Simply spoken, there is no sense in what you are doing here.
The java generic T you are using there is a compile time feature. It allows you to use more specific types at compile time, instead of using Object all over the place.
Therefore you can't use generics to determine a "T" at "runtime", as you probably intend to. That T in your method comes from the "outside", and the compiler determines that in occasion it should be a ResponseModel<Integer> and ResponseModel<Whatever> in another context.
You can't have gson read JSON data to return a specific ResponseModel<Whatever> for you. If at all, you might be able to use TypeAdapter magic that does some switching based on the actual value, to return this or that specific ResponseModel<Foo>.
Beyond that: when using such bean like classes as your ResponseModel, you simply want them to be specific, not generic.
Not sure, but I had a similar requirement, but its for Android though. Here is the reference link, where I had to write a generic class to load the different forms of JSON from Assets folder and parse to POJO class.
https://github.com/gokulnathperiasamy/Android-Helper/blob/master/JSONHelper.java
Code:
private static String convertJSONtoEntity(String jsonString, String typeString) {
String jsonObjectString = null;
try {
JSONObject jsonObject = new JSONObject(jsonString);
jsonObjectString = jsonObject.get(typeString).toString();
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
return jsonObjectString;
}
private static <T> List<T> fromJsonList(String json, Class<T> clazz) {
Object[] array = (Object[]) java.lang.reflect.Array.newInstance(clazz, 0);
array = new Gson().fromJson(json, array.getClass());
List<T> list = new ArrayList<>();
if (array != null && array.length > 0) {
for (Object anArray : array) {
list.add(clazz.cast(anArray));
}
}
return list;
}
Usage:
Invoke convertJSONtoEntity() with your jsonString and typeString will be your root element of your JSON.
Invoke the fromJsonList() with value returned by convertJSONtoEntity() and Class. This gives list of objects from JSON.
With the help of other answers and further searching in Google, I ended up with the following code
public static <T> ResponseModel<T> Get(Class<?> classType, String requestUri) throws ClientProtocolException, IOException
{
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(requestUri);
httpGet.addHeader("accept", "application/json");
HttpResponse response = client.execute(httpGet);
ResponseModel<T> responseModel = new ResponseModel<T>();
if (response.getStatusLine().getStatusCode() == 200)
{
responseModel.setObject((T) Utils.fromJson(EntityUtils.toString(response.getEntity()), classType));
responseModel.setIsRequestSuccessful(true);
}
else
{
responseModel.setMessage(response.getEntity().getContent().toString());
responseModel.setIsRequestSuccessful(false);
}
return responseModel;
}
We have created the Jersy REST service.
We are returning response object with 10 String fields and one Hash Map in format specified in below code snippet.
We could receive all the 10 string field values.
We can also see the HashMap with key/values when access it from client program.
CustomObject in HashMap became String when access it from client program.
so it throws exception
Listcom.tetherfi.restful.model.AppTypeData#2dc0f069
Test
err: java.lang.ClassCastException: java.lang.String cannot be cast to com.tetherfi.restful.model.AppTypeData.
We use Moxy to do the mapping. Please advise whether there is a way to retain the object value.
Response object from Jersy service
#XmlRootElement
public class ServiceRespParams {
private String param1;
private String param2;
private String param3;
private String param4;
private String param5;
private HashMap<String,ArrayList<AppTypeData>>hm;
public HashMap<String, ArrayList<AppTypeData>> getHm() {
return hm;
}
public void setHm(HashMap<String, ArrayList<AppTypeData>> hm) {
this.hm = hm;
}
Client code
public class Client {
public ServiceResponse postRequest(ServiceRequest request) {
Logger logger = Logger.getLogger("JerseyClient");
ServiceResponse serviceResponse = null;
WebTarget webTarget = null;
Response response = null;
// creating client with MOXy JSON
Client client = ClientBuilder.newClient();
try {
String url = "http://localhost:9080/Rest_ProxyLayer/webapi/doc01";
logger.info("Sending Request.... "+url);
webTarget = client.target(url).property(ClientProperties.CONNECT_TIMEOUT, 1000)
.property(ClientProperties.READ_TIMEOUT, 1000);
logger.info("Request Sent...");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
response = invocationBuilder.get();
logger.info("Response Status :"+response.getStatus());
serviceResponse = response.readEntity(ServiceResponse.class);
System.out.println("Response from service"+ serviceResponse.getHm().get("CreditCard").get(0).getParam1());
} catch (Exception e) {
logger.error("Error!! Check connection..", e);
e.printStackTrace();
}
return serviceResponse;
}
}
It throws class cast exception when try to access the method.
System.out.println("Response from service"+ serviceResponse.getHm().get("CreditCard").get(0).getParam1());
Output from Rest service. Hashmap entries are below. It shows object but it seems to be converted to string.
HM: {CreditCard=[com.tetherfi.restful.model.AppTypeData#1b6c278e, com.tetherfi.restful.model.AppTypeData#208f5a00]}
Thanks
Vinoth
I wrote a test that uses Mockito 1.9.5. I have an HttpGet and an HttpPost which an HttpClient execute, and I'm testing to verify that the response from each returns the expected result in the form of an input stream.
The issue is that while using
Mockito.when(mockedClient.execute(any(HttpPost.class))).thenReturn(postResponse) and Mockito.when(mockedClient.execute(any(HttpGet.class))).thenReturn(getResponse), where the responses are different objects, mockedClient.execute() always returns getResponse.
The test I've written is below:
private InputStream postContent;
private InputStream getContent;
#Mock
private FilesHandler hand;
#Mock
private MockHttpClient client;
private MockHttpEntity postEntity;
private MockHttpEntity getEntity;
private MockHttpResponse postResponse;
private MockHttpResponse getResponse;
private String imgQuery = "someQuery";
private ParametersHandler ph;
private FileHandlerImpl fileHand;
private String indexFolder = "src/test/resources/misc/";
private String indexFile = "index.csv";
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
try {
postContent = new FileInputStream(indexFolder + "testContent.txt");
postEntity = new MockHttpEntity(postContent);
postResponse = new MockHttpResponse(postEntity, new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "postReasonPhrase"));
getContent = new FileInputStream(indexFolder + "testContent.txt");
getEntity = new MockHttpEntity(getContent);
getResponse = new MockHttpResponse(getEntity, new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "getReasonPhrase"));
ph = new ParametersHandler();
fileHand = new FileHandlerImpl(client, ph, indexFolder, indexFile);
} catch (Exception e) {
failTest(e);
}
}
#Test
public void getFileWhenEverythingJustWorks() {
try {
Mockito.when(client.execute(Mockito.any(HttpPost.class))).thenReturn(postResponse);
Mockito.when(client.execute(Mockito.any(HttpGet.class))).thenReturn(getResponse);
fileHand.getFile(hand, imgQuery, ph, "I");
Mockito.verify(hand).rebuildIndex(Mockito.any(String.class), Mockito.any(Map.class), Mockito.any(Map.class), hand);
} catch (IOException e) {
failTest(e);
}
}
A shortened version of the method being tested is below.
public void getFile(FilesHandler fileHandlerFl, String query, ParametersHandler ph, String type) {
JsonFactory factory = new JsonFactory();
HttpPost post = preparePost(query, factory);
CloseableHttpResponse response = client.execute(post);
String uri = "https://someuri.com" + "/File";
HttpGet get = new HttpGet(uri);
setParameters(get);
response.close();
response = client.execute(get);
}
As always, any help you can provide is appreciated.
Despite what it would mean when read in English, in Mockito 1.x, any(HttpGet.class) matches any value and not just any HttpGet. The parameter is only used to save a cast previous to Java 8.
From the Matchers.any(Class) documentation:
Matches any object, including nulls
This method doesn't do type checks with the given parameter, it is only there to avoid casting in your code. This might however change (type checks could be added) in a future major release.
Use isA(HttpGet.class) and isA(HttpPost.class) instead, and see Brice's comment below about future changes to the any(Class<?> clazz) matcher.
I am new to Android development. Here, I am making a GET call like this -
protected String doInBackground(String... params) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("email", "guest#example.com"));
JSONHttpClient jsonHttpClient = new JSONHttpClient();
ProductDetail[] products = jsonHttpClient.Get(ServiceUrl.PRODUCT, nameValuePairs, ProductDetail[].class);
return null;
}
This is the GET call in JSONHttpClient file -
public <T> T Get(String url, List<NameValuePair> params, final Class<T> objectClass) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Accept-Encoding", "gzip");
httpGet.setHeader("Authorization", "Bearer <code>");
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream inputStream = httpEntity.getContent();
Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
String resultString = convertStreamToString(inputStream);
inputStream.close();
return new GsonBuilder().create().fromJson(resultString, objectClass);
}
return null;
}
And this is my ProductDetail class -
public class ProductDetail {
public int Id;
public String Name;
}
On running this, I am getting below error -
No-args constructor for class com.compa.ProductDetail does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
This is thrown on this line in JSONHttpClient file -
return new GsonBuilder().create().fromJson(resultString, objectClass);
Can anyone help on this?
In my web api, I am creating json like this (proddetails is a C# IEnumerable object) -
json = JsonConvert.SerializeObject(proddetails);
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return response;
The structure of response json is -
[
{
"Id": 1,
"Name": "First"
},
{
"Id": 2,
"Name": "Second"
}
]
The Gson user guide (https://sites.google.com/site/gson/gson-user-guide) tells you that a well behaved class (meant for serialization and deserialization) should have a no argument constructor. If this is not there, it advises you to use InstanceCreator.
Even if you do not have a constructor, Gson will create an ObjectConstructor for your class. But this is not safe always and has it's own limitations. This question on SO goes more into the details: Is default no-args constructor mandatory for Gson?
NOTE: Please see that if this is an inner class, then it MUST have a constructor as explained in the documentation.
EDIT: Your json is an array. So you need to have the specified number of array objects in the containing class. So you can do the following and then cast:
public class ProductDetailArray {
public ProductDetailArray[] array;
public static ProductDetail {
public ProductDetail() {} // You can also make the constructor private if you don't want anyone to instantiate this
public int Id;
public String Name;
}
}
Once you cast your json similarly as before:
ProductDetailArray obj = GsonBuilder.create().fromJson(response, ProductDetailArray.class);
ProductDetail one = obj.array[0];
ProductDetail two = obj.array[1];
And then you can do your manipulation.. also you should probably be using Gson.fromJson() rather than the GsonBuilder
I have written a REST web service in Netbean IDE using Jersey Framework and Java.
For every request the user needs to provide a username and a password, I know that this authentication is not a best practice (using a curl command like: curl -u username:password -X PUT http://localhsot:8080/user).
Now I want to call a REST web service from an Android Class.
How should I do it?
I have an Android Class which uses DefaultHttpClient and CredentialUsernameAndPassword, but when I run it in Eclipse, sometimes I get a runtime exception or SDK exception.
This is an sample restclient class
public class RestClient
{
public enum RequestMethod
{
GET,
POST
}
public int responseCode=0;
public String message;
public String response;
public void Execute(RequestMethod method,String url,ArrayList<NameValuePair> headers,ArrayList<NameValuePair> params) throws Exception
{
switch (method)
{
case GET:
{
// add parameters
String combinedParams = "";
if (params!=null)
{
combinedParams += "?";
for (NameValuePair p : params)
{
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
if (combinedParams.length() > 1)
combinedParams += "&" + paramString;
else
combinedParams += paramString;
}
}
HttpGet request = new HttpGet(url + combinedParams);
// add headers
if (headers!=null)
{
headers=addCommonHeaderField(headers);
for (NameValuePair h : headers)
request.addHeader(h.getName(), h.getValue());
}
executeRequest(request, url);
break;
}
case POST:
{
HttpPost request = new HttpPost(url);
// add headers
if (headers!=null)
{
headers=addCommonHeaderField(headers);
for (NameValuePair h : headers)
request.addHeader(h.getName(), h.getValue());
}
if (params!=null)
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
executeRequest(request, url);
break;
}
}
}
private ArrayList<NameValuePair> addCommonHeaderField(ArrayList<NameValuePair> _header)
{
_header.add(new BasicNameValuePair("Content-Type","application/x-www-form-urlencoded"));
return _header;
}
private void executeRequest(HttpUriRequest request, String url)
{
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try
{
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
instream.close();
}
}
catch (Exception e)
{ }
}
private static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
}
catch (IOException e)
{ }
return sb.toString();
}
}
Recently discovered that a third party library - Square Retrofit can do the job very well.
Defining REST endpoint
public interface GitHubService {
#GET("/users/{user}/repos")
List<Repo> listRepos(#Path("user") String user,Callback<List<User>> cb);
}
Getting the concrete service
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.build();
GitHubService service = restAdapter.create(GitHubService.class);
Calling the REST endpoint
List<Repo> repos = service.listRepos("octocat",new Callback<List<User>>() {
#Override
public void failure(final RetrofitError error) {
android.util.Log.i("example", "Error, body: " + error.getBody().toString());
}
#Override
public void success(List<User> users, Response response) {
// Do something with the List of Users object returned
// you may populate your adapter here
}
});
The library handles the json serialization and deserailization for you. You may customize the serialization and deserialization too.
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setConverter(new GsonConverter(gson))
.build();
Stop with whatever you were doing ! :)
Implement the RESTful client as a SERVICE and delegate the intensive network stuff to activity independent component: a SERVICE.
Watch this insightful video
http://www.youtube.com/watch?v=xHXn3Kg2IQE where Virgil Dobjanschi is explaining his approach(es) to this challenge...
Using Spring for Android with RestTemplate
https://spring.io/guides/gs/consuming-rest-android/
// The connection URL
String url = "https://ajax.googleapis.com/ajax/" +
"services/search/web?v=1.0&q={query}";
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP GET request, marshaling the response to a String
String result = restTemplate.getForObject(url, String.class, "Android");
I used OkHttpClient to call restful web service. It's very simple.
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = httpClient.newCall(request).execute();
String body = response.body().string()
What back-end? If JAVA then you can use REST with Java (JAX-RS) using Jersey.
On the Android side you can use this simple RestClient to work with that REST service.
For JSON <--> Object mapping on both sides (Android, Java back-end) you can use GSON.
Perhaps am late or maybe you've already used it before but there is another one called ksoap and its pretty amazing.. It also includes timeouts and can parse any SOAP based webservice efficiently. I also made a few changes to suit my parsing.. Look it up
Follow the below steps to consume RestFul in android.
Step1
Create a android blank project.
Step2
Need internet access permission. write the below code in AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
Step3
Need RestFul url which is running in another server or same machine.
Step4
Make a RestFul Client which will extends AsyncTask. See RestFulPost.java.
Step5
Make DTO class for RestFull Request and Response.
RestFulPost.java
package javaant.com.consuming_restful.restclient;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.util.Map;
import javaant.com.consuming_restful.util.Util;
/**
* Created by Nirmal Dhara on 29-10-2015.
*/
public class RestFulPost extends AsyncTask<map, void,="" string=""> {
RestFulResult restFulResult = null;
ProgressDialog Asycdialog;
String msg;
String task;
public RestFulPost(RestFulResult restFulResult, Context context, String msg,String task) {
this.restFulResult = restFulResult;
this.task=task;
this.msg = msg;
Asycdialog = new ProgressDialog(context);
}
#Override
protected String doInBackground(Map... params) {
String responseStr = null;
Object dataMap = null;
HttpPost httpost = new HttpPost(params[0].get("url").toString());
try {
dataMap = (Object) params[0].get("data");
Gson gson = new Gson();
Log.d("data map", "data map------" + gson.toJson(dataMap));
httpost.setEntity(new StringEntity(gson.toJson(dataMap)));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
DefaultHttpClient httpclient= Util.getClient();
HttpResponse response = httpclient.execute(httpost);
int statusCode = response.getStatusLine().getStatusCode();
Log.d("resonse code", "----------------" + statusCode);
if (statusCode == 200)
responseStr = EntityUtils.toString(response.getEntity());
if (statusCode == 404) {
responseStr = "{\n" +
"\"status\":\"fail\",\n" +
" \"data\":{\n" +
"\"ValidUser\":\"Service not available\",\n" +
"\"code\":\"404\"\n" +
"}\n" +
"}";
}
} catch (Exception e) {
e.printStackTrace();
}
return responseStr;
}
#Override
protected void onPreExecute() {
Asycdialog.setMessage(msg);
//show dialog
Asycdialog.show();
super.onPreExecute();
}
#Override
protected void onPostExecute(String s) {
Asycdialog.dismiss();
restFulResult.onResfulResponse(s,task);
}
}
For more details and complete code please visit http://javaant.com/consume-a-restful-webservice-in-android/#.VwzbipN96Hs
Here is my Library That I have created for simple Webservice Calling,
You can use this by adding a one line gradle dependency -
compile 'com.scantity.ScHttpLibrary:ScHttpLibrary:1.0.0'
Here is the demonstration of using.
https://github.com/vishalchhodwani1992/httpLibrary