Last active
March 23, 2018 16:53
-
-
Save hbb20/d77643801cdb93cc0ecd908766713f91 to your computer and use it in GitHub Desktop.
Use this class in your walmart demo app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is the list of resources that will be helpful for AwesomeWalmartApp: | |
Image resources: https://goo.gl/S9YQDP | |
Method to get URL content during background task: Included in this page. | |
Model Class for Product : Included in this page (Note: this class contatins method to convert JSON Data Array to List of Products) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String getWebPageContent(String pageLink) { | |
InputStream stream = null; | |
HttpsURLConnection connection = null; | |
String result = null; | |
try { | |
//this is just to see the loading during api fetch. Never include this in real apps. | |
//it simply waits for 5 sec before doing anything. | |
Thread.sleep(5000); | |
URL urlObject = new URL(pageLink); | |
connection = (HttpsURLConnection) urlObject.openConnection(); | |
// Timeout for reading InputStream arbitrarily set to 3000ms. | |
connection.setReadTimeout(3000); | |
// Timeout for connection.connect() arbitrarily set to 3000ms. | |
connection.setConnectTimeout(3000); | |
// For this use case, set HTTP method to GET. | |
connection.setRequestMethod("GET"); | |
// Already true by default but setting just in case; needs to be true since this request | |
// is carrying an input (response) body. | |
connection.setDoInput(true); | |
// Open communications link (network traffic occurs here). | |
connection.connect(); | |
int responseCode = connection.getResponseCode(); | |
if (responseCode != HttpsURLConnection.HTTP_OK) { | |
throw new IOException("HTTP error code: " + responseCode); | |
} | |
// Retrieve the response body as an InputStream. | |
stream = connection.getInputStream(); | |
if (stream != null) { | |
// Converts Stream to String. | |
Reader reader = null; | |
final int bufferSize = 1024; | |
final char[] buffer = new char[bufferSize]; | |
final StringBuilder out = new StringBuilder(); | |
reader = new InputStreamReader(stream, "UTF-8"); | |
for (; ; ) { | |
int rsz = reader.read(buffer, 0, buffer.length); | |
if (rsz < 0) | |
break; | |
out.append(buffer, 0, rsz); | |
} | |
result = out.toString(); | |
} | |
}catch (IOException e){ | |
e.printStackTrace(); | |
}catch (InterruptedException e){ | |
e.printStackTrace(); | |
} | |
finally { | |
// Close Stream and disconnect HTTPS connection. | |
if (stream != null) { | |
try { | |
stream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (connection != null) { | |
connection.disconnect(); | |
} | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import com.google.gson.annotations.Expose; | |
import com.google.gson.annotations.SerializedName; | |
import com.google.gson.reflect.TypeToken; | |
import java.lang.reflect.Type; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Product { | |
// TODO: 12/20/17 add (implementation 'com.google.code.gson:gson:2.2.4') in your build.gradle file | |
//this url will give list of products | |
public static String WALMART_PRODUCTS_URL = "https://demo1738991.mockable.io/walmart-products"; | |
@SerializedName("name") | |
@Expose | |
private String name; | |
@SerializedName("description ") | |
@Expose | |
private String description; | |
@SerializedName("price") | |
@Expose | |
private float price; | |
@SerializedName("imageURL") | |
@Expose | |
private String imageURL; | |
public static List<Product> getProductListFromJsonArray(String jsonProductArray) { | |
List<Product> products = new ArrayList<>(); | |
//create gson object | |
Gson gson = new Gson(); | |
//create list type | |
Type productListType = new TypeToken<List<Product>>() { | |
}.getType(); | |
//convert json to list | |
try { | |
products = gson.fromJson(jsonProductArray, productListType); | |
} catch (Exception e) { | |
Log.d("Product", "getProductListFromJsonArray: could not convert:" + jsonProductArray); | |
} | |
return products; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public float getPrice() { | |
return price; | |
} | |
public void setPrice(float price) { | |
this.price = price; | |
} | |
public String getImageURL() { | |
return imageURL; | |
} | |
public void setImageURL(String imageURL) { | |
this.imageURL = imageURL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment