Last active
September 14, 2018 10:51
-
-
Save hussaintamboli/523004b1b7ad806a66cc861e7bebc302 to your computer and use it in GitHub Desktop.
Web Snippets for Pinn
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
<!-- START OF USER'S CODE --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<!-- USER COPIES BELOW CODE SNIPPET AND ADDS TO HIS APP --> | |
<div id="app123"> | |
</div> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
fetch('http://www.mocky.io/v2/5b614e96300000d9046a404c') | |
.then(function(response) { | |
return response.json(); | |
}).then(function(data) { | |
var div = document.getElementById("app123"); | |
div.style["text-align"] = "center"; | |
div.style["color"] = "red"; | |
var span = document.createElement('span'); | |
span.appendChild(document.createTextNode(data.title)); | |
div.appendChild(span); | |
var p = document.createElement('p'); | |
p.appendChild(document.createTextNode(data.details)); | |
div.appendChild(p); | |
}).catch(function(err) { | |
console.log(err.stack); | |
}); | |
</script> | |
<!-- END OF CODE SNIPPET --> | |
</body> | |
</html> | |
<!-- END OF USER'S CODE --> |
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
<!-- START OF USER'S CODE --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<!-- USER COPIES BELOW CODE SNIPPET AND ADDS TO HIS APP --> | |
<div id="app123"> | |
</div> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
fetch('http://www.mocky.io/v2/5b614e96300000d9046a404c') | |
.then(function(response) { | |
return response.json(); | |
}).then(function(data) { | |
var div = document.getElementById("app123"); | |
div.classList.add("panel"); | |
div.classList.add("panel-default"); | |
var headDiv = document.createElement('div'); | |
headDiv.classList.add("panel-heading"); | |
headDiv.appendChild(document.createTextNode(data.title)); | |
div.appendChild(headDiv); | |
var bodyDiv = document.createElement('div'); | |
bodyDiv.classList.add("panel-body"); | |
bodyDiv.appendChild(document.createTextNode(data.details)); | |
div.appendChild(bodyDiv); | |
}).catch(function(err) { | |
console.log(err.stack); | |
}); | |
</script> | |
<!-- END OF CODE SNIPPET --> | |
</body> | |
</html> | |
<!-- END OF USER'S CODE --> |
PS: I know this is not the right place.
But here is the android snippet.
/**
* Use the library compile 'com.squareup.okhttp3:okhttp:3.4.1' {or whichever is the latest version}
*
* 1. make get call to url.
* 2. parse data
* 3. apply {mentioned method, onApiError, onApiSuccess} in activity or fragment
*
* Note: Since this is an anonmyous asynctask, do reference it and cancel it on onPause etc.
*/
new AsyncTask<Void, Void, Void>() {
public String errorMessage;
public long epoch;
public String title;
public String details;
public boolean isApiSuccesfull;
public static final String URL = "http://www.mocky.io/v2/5b614e96300000d9046a404c";
public static final String TAG = "Maintenence API";
@Override
protected Void doInBackground(Void... voids) {
new OkHttpClient().newCall(getRequest()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG,e.getMessage());
setFailed(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String responseString = response.body().string();
Log.d(TAG,"Api response"+responseString);
try {
parseResponse(responseString);
} catch (JSONException e) {
setFailed(e.getMessage());
e.printStackTrace();
}
}
});
return null;
}
private void setFailed(String message) {
isApiSuccesfull = false;
errorMessage = message;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(!isApiSuccesfull){
onApiError(errorMessage);
}else{
onApiSuccess(title,details,epoch);
}
}
private void parseResponse(String jsonResponse) throws JSONException {
JSONObject json = new JSONObject(jsonResponse);
epoch = json.getLong("event_epoch");
title = json.getString("title");
details = json.getString("details");
isApiSuccesfull = true;
}
private Request getRequest() {
Request request = new Request.Builder()
.url(URL)
.build();
return request;
}
/**
* Overrides for Hussains mock downtime api
*/
private void onApiError(String message) {
//Override at your leisure, call your activity or fragment code.
toast(message);
}
private void onApiSuccess(String title, String details, long epoch) {
//Override at your leisure, call your activity or fragment code.
toast(title+" "+details+" "+DateUtils.getDate(epoch,"DD/MM/yyyy"));
}
}.execute();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The basic snippet will render a warning message at the top of the webpage.