I am using WordPress rest API to fetch data and I am only fetching 10 posts at a time using volley.
API Link
When I request API in the browser it gives me a result under 6-7 seconds...
But my app takes more than 15 sec to show that 10 posts.
Sometimes it takes more than 1 minute to show the data.
I even added a parameter to get only 2 posts per page. But the response time is the same.
I am searching and modifying code from 2 days but can't get rid of it.
Here is the code:-
private void jsonrequest(int pageNo) {
JSON_URL = "https://jamuitoday.com/wp-json/wp/v2/posts?_embed&page=" + pageNo;
JsonArrayRequest request = new JsonArrayRequest(JSON_URL, response -> {
JSONObject jsonObject;
for (int i = 0; i < response.length(); i++) {
try {
isLoading = true;
isNewsLoaded = false;
jsonObject = response.getJSONObject(i);
News news = new News();
//Get News Link
String link = jsonObject.getString("link");
news.setLink(link);
//Set Date
String date = jsonObject.getString("date");
String d = date.substring(0, 10);
int time = Integer.parseInt(date.substring(11, 13));
if (time > 12) {
String ti = date.substring(13, 16);
String dt = d + " " + (time - 12) + ti + " PM";
news.setdate(dt);
} else {
String ti = date.substring(11, 16);
String dt = d + " " + ti + " AM";
news.setdate(dt);
}
//Set Title
JSONObject title = jsonObject.getJSONObject("title");
String titleren = title.getString("rendered");
news.setName(titleren);
//Set Description
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
JSONObject desc = jsonObject.getJSONObject("content");
String descSub = desc.getString("rendered");
news.setDescription(String.valueOf(Html.fromHtml((descSub))));
news.setAdImgUrl(descSub);
// Set Image
JSONObject img = new JSONObject(jsonObject.getString("_embedded"));
JSONArray ttt = img.getJSONArray("wp:featuredmedia");
String s = "";
for (int t = 0; t < ttt.length(); t++) {
JSONObject obj = ttt.getJSONObject(t);
s = obj.getString("source_url");
}
news.setImage_url(s);
listNews.add(news);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(listNews);
isLoading = false;
isNewsLoaded = true;
pageNumber += 1;
if (recyclerView.getLayoutManager() != null) {
recyclerView.getLayoutManager().scrollToPosition(currentListPosition);
}
if (shimmerFrameLayout.isShimmerVisible()) {
shimmerFrameLayout.stopShimmer();
shimmerFrameLayout.setVisibility(View.GONE);
}
if (swipyRefreshLayout.isRefreshing()) {
swipyRefreshLayout.setRefreshing(false);
}
}, error -> {
});
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(request);
}
Any hack that can speed it up?
I'll implement SQLite to store data and show at app startup. So, the user can't feel the load time.
Related
Im using API for sending SMS of product query and taking variable from properties file in java.
I need to convert the output message to utf-8 as i need to send SMS of Persian language, please help me.
here is the code:
List<String> returnMessage = new ArrayList<String>();
Properties messagePack = new Properties();
messagePack.load(RoshanSendSMS.class.getClassLoader().getResourceAsStream(String.format("%spack.properties", language)));
JSONObject parentObject = new JSONObject(result);
JSONArray objArray = (JSONArray) parentObject.get("PurchasedOfferArray");
int size = objArray.length();
for (int i = 0; i < size; i++) {
JSONObject obj = (JSONObject) objArray.get(i);
if (!obj.has("CatalogItemId")) {
continue;
}
String catalogId = obj.get("CatalogItemId").toString();
String productId = messagePack.getProperty(catalogId);
if (productId == null) {
continue;
}
String newProductId = "ProductName" + productId.split("t")[1];
String msg = messagePack.getProperty(newProductId);
if (msg == null) {
continue;
}
if (!returnMessage.contains(msg)) {
returnMessage.add(msg);
}
}
if(returnMessage.size()>0){
returnMessage.add(0, messagePack.getProperty("PRE_MSG"));
String fullMsg = String.join(" ", returnMessage);
System.out.println("Message: "+ fullMsg);
}else{
System.out.println("No message found");
}
return "SUCCESS";
I am trying to fetch campaign,ad level information from Facebook Using Graph API.
Campaign level information is getting with zero error for all accounts.
But for the Ad Level info, I am not getting the entire data. The API returns only 50. Remaining ads are getting with an exception called "Internal Server Error".
I've been getting this problem all day for one particular account. Every other Account works fine.
Below is the HTTP response:
response = (okhttp3.Response) Response{protocol=http/1.1, code=500, message=Internal Server Error,
url=https://graph.facebook.com/v3.3/act_fbAccountId/ads?access_token=myAccessToken&fields=%5B%22account_id%22%2C%22campaign_id%22%2C%22adset_id%22%2C%22id%22%2C%22bid_amount%22%2C%22name%22%2C%22status%22%2C%22preview_shareable_link%22%5D&limit=25&after=QVFIUkFzZAVdONXEwMXc5NjlOWURwczRQN0V3QW12NndRa0I4ZAUxsSjNkZA0FTRHR1U1dnaTQ5MTh2QnJ3bzNwWkNPd21jbC1tbjRmZAF81WVBsc0txaERCMHVn}
Below is the code
public List facebookAdsSynchThroughJson(String accessTokens, Long advertAccId, Properties googleStructureProperties, byte isOnScheduler, String appSecret) {
this.access_tokens = accessTokens;
this.appSecret = appSecret;
APIContext context = new APIContext(accessTokens, appSecret).enableDebug(true);
List<FacebookAdsStructure> adsList = new ArrayList<>();
String ads = "";
JSONObject advertIdObj = null;
JSONArray dataList = null;
ObjectMapper mapper = null;
boolean cond = true;
boolean success = false;
int limitValue = 100;
try {
while (cond) {
try {
AdAccount adAccount = new AdAccount(advertAccId, context).get().execute();
List adsFields = new ArrayList();
adsFields.add("account_id");
adsFields.add("campaign_id");
adsFields.add("adset_id");
adsFields.add("id");
adsFields.add("bid_amount");
adsFields.add("name");
adsFields.add("status");
adsFields.add("preview_shareable_link");
Map<String, Object> adsmap = new HashMap<>();
adsmap.put("fields", adsFields);
adsmap.put("limit", limitValue);
ads = adAccount.getAds().setParams(adsmap).execute().getRawResponseAsJsonObject().toString();
} catch (APIException ex) {
LOGGER.info("Exception thrown when fetching ads for Account Id: " + advertAccId + " with limit value: " + limitValue);
LOGGER.error(ex);
if (limitValue > 6) {
cond = true;
limitValue = limitValue / 2;
} else {
cond = false;
success = false;
break;
}
}
if (!ads.isEmpty()) {
cond = false;
success = true;
}
}
if (success) {
advertIdObj = new JSONObject(ads);
dataList = advertIdObj.getJSONArray("data");
mapper = new ObjectMapper();
adsList = mapper.readValue(dataList.toString(), new TypeReference<List<FacebookAdsStructure>>() {
});
JsonObject obj;
JsonParser parser = new JsonParser();
JsonElement result = parser.parse(ads);
obj = result.getAsJsonObject();
int maxTries = 1;
if (obj.has("data") && maxTries <= 5) {
while (obj.has("paging") && maxTries <= 5) {
JsonObject paging = obj.get("paging").getAsJsonObject();
if (paging.has("cursors")) {
JsonObject cursors = paging.get("cursors").getAsJsonObject();
String before = cursors.has("before") ? cursors.get("before").getAsString() : null;
String after = cursors.has("after") ? cursors.get("after").getAsString() : null;
}
String previous = paging.has("previous") ? paging.get("previous").getAsString() : null;
String next = paging.has("next") ? paging.get("next").getAsString() : "empty";
if (!next.equalsIgnoreCase("empty")) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
Request request1 = new Request.Builder().url(next).get().build();
Response response = client.newCall(request1).execute();
if (response.isSuccessful()) {
maxTries = 1;
ads = response.body().string();
advertIdObj = new JSONObject(ads);
dataList = advertIdObj.getJSONArray("data");
mapper = new ObjectMapper();
List<FacebookAdsStructure> adsLists = new ArrayList<>();
adsLists = mapper.readValue(dataList.toString(), new TypeReference<List<FacebookAdsStructure>>() {
});
adsList.addAll(adsLists);
LOGGER.info("Fetched the Ad Structure.." + adsList.size());
parser = new JsonParser();
result = parser.parse(ads);
obj = result.getAsJsonObject();
} else {
LOGGER.info("Exception in response when fetching ads for Account Id: " + advertAccId + " , error response message: " + response.message() + " and with limit value: " + limitValue);
maxTries++;
if (maxTries >= 6) {
break;
}
try {
LOGGER.info("Thread Sleeping For 2 Minutes - Started for fb se_account_id " + advertAccId + " and maxTries = " + maxTries);
Thread.sleep(120000);
LOGGER.info("Thread Sleeping For 2 Minutes - Completed for fb se_account_id " + advertAccId + " and maxTries = " + maxTries);
} catch (InterruptedException ex) {
Logger.getLogger(FacebookAccountStructureSyncThroughJson.class.getName()).log(Level.SEVERE, null, ex);
}
}
} else {
obj = new JsonObject();
}
}
}
}
} catch (JsonSyntaxException | IOException | JSONException ex) {
LOGGER.error(ex);
}
return adsList;
}
If anybody knows anything about this issue, please help me.
I'm new to Java and having problems with parsing json. I have a json file in res folder and need to get lat/lng from the file which will be displayed with a marker later. How can I parse the file within public void without needing to create a new Java Class or Activity?
Json
{
"type":"FeatureCollection",
"crs":{
"type":"name",
"properties":{
"name":"urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features":[
{
"type":"Feature",
"properties":{
"MEAN_X":13.34994,
"MEAN_Y":52.54291,
"UID":"B154"
},
"geometry":{
"type":"Point",
"coordinates":[
13.34993674,
52.54291394
]
}
},
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
JSONObject jsonObj = new JSONObject(R.raw.level1_points);
final String TAG_FEATURES = jsonObj.getString("features");
final String TAG_PROPERTIES = jsonObj.getString("properties");
final double TAG_MEANX = jsonObj.getDouble("MEAN_X");
final double TAG_MEANY = jsonObj.getDouble("MEAN_X");
final String TAG_UID = jsonObj.getString("UID");
try {
JSONArray features = jsonObj.getJSONArray(TAG_FEATURES);
for (int i = 0; i < features.length(); i++) {
// Create a marker for each room in the JSON data.
JSONObject c = features.getJSONObject(i);
JSONObject properties = c.getJSONObject(TAG_PROPERTIES);
Double MEAN_X = properties.getDouble(TAG_MEANX);
Double MEAN_Y = properties.getDouble(TAG_MEANY);
String UID = properties.getString(TAG_UID);
if (spinner.getSelectedItem().toString().equals(UID)) {
LatLng room = new LatLng(MEAN_X; MEAN_Y);
mMap.addMarker(new MarkerOptions().position(raum).title("Room"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(room));
mMap.animateCamera(CameraUpdateFactory.zoomTo(16));
<code>
you can do the following changes
first you'll have to get the json file from the res folder
and then parse the json data
// get the json file
InputStream inputStream = getResources().openRawResource(R.raw.level1_points);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int ctr;
try {
ctr = inputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = inputStream.read();
}
inputStream.close();
String jsonString = byteArrayOutputStream.toString();
Log.v("Text Data", jsonString);
JSONObject jsonObj = new JSONObject(jsonString);
JSONArray features = jsonObj.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
// Create a marker for each room in the JSON data.
JSONObject c = features.getJSONObject(i);
JSONObject properties = c.getJSONObject("properties");
Double MEAN_X = properties.getDouble("MEAN_X");
Double MEAN_Y = properties.getDouble("MEAN_Y");
String UID = properties.getString("UID");
Log.e(TAG, "onCreate: " + MEAN_X + MEAN_Y + UID);
}
} catch (Exception e) {
Log.e(TAG, "onCreate: " + e.getMessage());
}
I have a text editor where user can put text like this:
[[PAYMENTS_N]] You have to pay [[amount]]$ before [[date]].[[/PAYMENTS_N]]
[[PAYMENTS_Z]] You didn't pay [[debt]]$ on time: [[ddate]].[[/PAYMENTS_Z]]
And there are XML files that look like this:
<DATA_NZ><USER><ID>12345</ID><COMP_NZ><COMP>
<STATUS>Z</STATUS>
<AMOUNT_NZ>128.01</AMOUNT_NZ>
<DATE_NZ>28.05.2015</DATE_NZ>
<STATUS>N</STATUS>
<AMOUNT_NZ>12.32</AMOUNT_NZ>
<DATE_NZ>21.09.2015</DATE_NZ>
<STATUS>N</STATUS>
<AMOUNT_NZ>12.32</AMOUNT_NZ>
<DATE_NZ>20.10.2015</DATE_NZ>
</COMP></COMP_NZ></USER></DATA_NZ>
Now I want to change the text from editor into something like this:
Remember about your next payments:
You have to pay 12.32$ before 21.09.2015.
You have to pay 12.32$ before 20.10.2015.
You didn't pay 128.01$ on time: 28.05.2015.
And I have this code:
public void parseTags(String content) {
String nOpenTag, nCloseTag;
String zOpenTag, zCloseTag;
String dnTag, knTag, dzTag, kzTag;
nOpenTag = "[[PAYMENTS_N]]";
nCloseTag = "[[/PAYMENTS_N]]";
zOpenTag = "[[PAYMENTS_Z]]";
zCloseTag = "[[/PAYMENTS_Z]]";
dnTag = "[[amount]]";
knTag = "[[date]]";
dzTag = "[[debt]]";
kzTag = "[[ddate]]";
String textToExtract = "";
String textToExtract = "";
String textToReplace = "";
String parsedContent = content;//; = content;
if (content.contains(nOpenTag) || content.contains(zOpenTag)) {
for (int i = 0; i < getXMLContainer("SZUK_XM4.XML").getUsers().size(); i++) {
if (getXMLContainer("SZUK_XM4.XML").getUsers().get(i) != null) { //
contact = consentService.findByContactTypeAndIdNumber(ContactType.EMAIL, getXMLContainer("SZUK_XM4.XML").getUsers().get(i).getIdNumber());
payments = getXMLContainer("SZUK_XM4.XML").getUsers().get(i).getPaymentContainer().getPayments();
if (contact.size() > 0) {
StringBuilder strB = new StringBuilder();
StringBuilder strB2 = new StringBuilder();
String textToReplace;
for (int c = 0; c < contact.size(); c++) {
strB.setLength(0);
for (int p = 0; p < payments.size(); p++) {
if (payments.get(p).getStatus().contains("N")) {
textToExtract = content.substring(content.indexOf(nOpenTag) + nOpenTag.length(), content.indexOf(nCloseTag));
textToReplace = textToExtract.replace(dnTag, "Day " + payments.get(p).getPaymentDate());
textToReplace = textToReplace.replace(knTag, "Amount " + payments.get(p).getPaymentAmount());
strB.append(textToReplace);
parsedContent = content.replace(textToExtract, strB.toString());
}
if (payments.get(p).getStatus().contains("Z")) {
textToExtract = parsedContent.substring(parsedContent.indexOf(zOpenTag) + zOpenTag.length(), parsedContent.indexOf(zCloseTag));
textToReplace = textToExtract.replace(dzTag, "Day " + payments.get(p).getPaymentDate());
textToReplace = textToReplace.replace(kzTag, "Amount " + payments.get(p).getPaymentAmount());
strB2.append(textToReplace);
parsedContent = parsedContent.replace(textToExtract, strB2.toString());
}
}
mailService.sendTemplateMail(contact.get(c).getContact(), parsedContent);
}
}
}
}
}
}
This code is supposed to find in the database user with given ID from XML.
If user exists then it reads all the data between and replaces [[TAGS]] from the text editor. After everything's done, it should send an email with replaced text.
The problem is that the text it sends isn't correctly replaced. It gives
Remember about your next payments: You have to pay 12.32$ before
21.09.2015. You have to pay 12.32$ before 20.10.2015.
You didn't pay [[debt]]$ on time: [[ddate]].
See the difference in the last line?
I'm using JAXB to read XML, if it matters.
Hope you can help me! Thanks.
replace:
parsedContent = content.replace(textToExtract, strB.toString());
with:
parsedContent = parsedContent.replace(textToExtract, strB.toString());
I am having trouble in running my Android app. It is a simple activity meant to get data from a remote database via HTTP.
I am getting these two errors in log cat:
Error parsing to json on getJarrayFromString();
org.json.JSONException: Value Database of type java.lang.String cannot
be converted to JSONArray.
And:
Error at fillproductlist(): java.lang.NullPointerException
I am not very good at Java/Android and this is first time I am asking this question here. I am unable to understand what layer of my app the problem might be in (my Java code, PHP, or database code)?
Code snapshot:
private void fillProductList() {
if (utils.isInternet()
&& CustomHttpClient.isAddressOk(Utils.LINK_PRODUCTS)) {
final int from = bid.size();
final int nr = 5;
long sqliteSize = db.size(mySqlDatabase.TABLE_BOOKS);
if (from == 0) {
// we add the post variables and values for the request
postParameters.add(new BasicNameValuePair("from", String
.valueOf(0).toString()));
postParameters.add(new BasicNameValuePair("nr", String.valueOf(
nr).toString()));
} else {
postParameters.add(new BasicNameValuePair("from", String
.valueOf(from).toString()));
postParameters.add(new BasicNameValuePair("nr", String.valueOf(
nr).toString()));
}
postParameters.add(new BasicNameValuePair("title", titleSearch));
postParameters.add(new BasicNameValuePair("code", codeSearch));
postParameters.add(new BasicNameValuePair("module", moduleSearch));
if (sortOrder != null)
postParameters.add(new BasicNameValuePair("order", sortOrder));
if (sortType != null)
postParameters.add(new BasicNameValuePair("by", sortType));
task = new RequestTask("books", postParameters);
task.setOnTaskCompleted(new OnTaskCompletedListener() {
public void onTaskStarted() {
if (!rlLoading.isShown()) {
rlLoading.startAnimation(fadeIn());
rlLoading.setVisibility(View.VISIBLE);
}
IS_PRODUCTS_TASK = true;
}
public void onTaskCompleted(String result) {
try {
if (result != "") {
// Enter the remote php link
// we convert the response into json array
jarray = utils.getJarrayFromString(result);
int mysqlSize = (jarray.getJSONObject(0)
.getInt("numRows"));
Log.i(DEBUG, "From " + from + " to " + mysqlSize);
if (from <= mysqlSize) {
int rows;
// we check to see if there is 0
if (jarray.length() > 0) {
Log.i(DEBUG,
"From "
+ from
+ " to "
+ Math.floor(mysqlSize / nr)
* nr);
if (from + 5 <= Math.floor(mysqlSize / nr)
* nr) {
rows = jarray.length();
} else {
rows = mysqlSize % nr + 1;
Utils.IS_ENDED_PRODUCT_LIST = true;
}
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < rows; i++) {
JSONObject row = jarray
.getJSONObject(i);
bid.add(row.getInt("bid"));
bTitle.add(row.getString("bTitle"));
bCode.add(row.getString("bCode"));
bPrice.add(row.getString("bPrice")
+ "£");
bDescription.add(row
.getString("bDescription"));
bModule.add(row.getString("bModule"));
bImage.add(Utils.PATH
+ row.getString("bImage"));
list.add(row.getString("bImage"));
// we check if this id already exists in the db, if it doesn't exists w create new one
if (!db.hasIDbooks(row.getInt("bid")))
db.createRowOnBooks(
row.getInt("bid"),
row.getString("bTitle"),
row.getString("bCode"),
row.getString("bPrice"),
row.getString("bDescription"),
row.getString("bModule"),
Utils.PATH
+ row.getString("bImage"),
row.getString("bSpecialOffer"),
row.getInt("bSpecialDiscount"),
row.getString("bDateAdded"));
Log.i(DEBUG,
row.getString("bDescription"));
}
new DownloadImages(list, bAdapter)
.execute();
}
}
postParameters.removeAll(postParameters);
} else {
Utils.IS_ENDED_PRODUCT_LIST = true;
if (rlLoading.isShown()) {
rlLoading.startAnimation(fadeOut());
rlLoading.setVisibility(View.INVISIBLE);
}
}
} catch (Exception e) {
Log.e(DEBUG,
"Error at fillProductList(): " + e.toString());
}
}
});
task.execute();
} else {
// if we are not connected on internet or somehow the link would not work, then we will take the rows stored in sqlite db
if (db.size(mySqlDatabase.TABLE_BOOKS) > 0) {
Cursor cursor = db.getBookssRows(mySqlDatabase.TABLE_BOOKS);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
bid.add(cursor.getInt(cursor
.getColumnIndex(mySqlDatabase.KEY_BID)));
bTitle.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BTITLE)));
bCode.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BCODE)));
bPrice.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BPRICE)) + "£");
bDescription.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BDESCRIPTION)));
bModule.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BMODULE)));
bImage.add(cursor.getString(cursor
.getColumnIndex(mySqlDatabase.KEY_BIMAGE)));
cursor.moveToNext();
}
bAdapter.notifyDataSetChanged();
Utils.IS_ENDED_PRODUCT_LIST = true;
}
}
}
In the OnTaskCompleted before
jarray = utils.getJarrayFromString(result);
add a log entry with
Log.i(DEBUG,result);
I am suspecting that you are getting a null value as result
if(result!="")
does not ensure a valid value.
Cheers