0
I developed an application in Java for Android, together I installed in my server in Hostinger Wordpress with the REST API. However, after running the application on my mobile, it can not download the Wordpress data. No errors in my code, and the path is correct.
package com.cunha.oapp.oapp.Tabs.TabsViews;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.cunha.oapp.oapp.DetailActivity;
import com.cunha.oapp.oapp.R;
import com.cunha.oapp.oapp.RecyclerView.RecyclerViewAdapters.DesignAdapter;
import com.cunha.oapp.oapp.RecyclerView.RecyclerViewClasses.Design;
import com.cunha.oapp.oapp.RecyclerView.RecyclerViewDecorations.DividerItemDecoration;
import com.cunha.oapp.oapp.RecyclerView.RecyclerViewUtils.ItemClickSupport;
import com.cunha.oapp.oapp.Tabs.TabsUtils.SlidingTabLayout;
import com.cunha.oapp.oapp.Utils.JsonParser;
import com.cunha.oapp.oapp.Utils.ScrollManagerToolbarTabs;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
public class TabDesignPattern extends Fragment {
String urlPost;
JSONObject jsonObjectDesignPosts;
JSONArray jsonArrayDesignContent;
ArrayList<Design> designs;
SwipeRefreshLayout swipeRefreshLayout;
String[] designTitle, designExcerpt, designImage, designImageFull, designContent;
int postNumber = 99;
Boolean error = false;
RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewAdapter;
View view;
SharedPreferences sharedPreferences;
Toolbar toolbar;
TypedValue typedValueToolbarHeight = new TypedValue();
SlidingTabLayout tabs;
int recyclerViewPaddingTop;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tab_design, container, false);
// Get shared preferences
sharedPreferences = getActivity().getSharedPreferences("VALUES", Context.MODE_PRIVATE);
// Setup RecyclerView News
recyclerViewDesign(view);
// Setup swipe to refresh
swipeToRefresh(view);
return view;
}
private void recyclerViewDesign(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewDesign);
tabs = (SlidingTabLayout) view.findViewById(R.id.tabs);
// Divider
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(android.R.drawable.divider_horizontal_bright)));
// improve performance if you know that changes in content
// do not change the size of the RecyclerView
recyclerView.setHasFixedSize(true);
// use a linear layout manager
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
urlPost = "http://cunha.esy.es/?json=1";
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
new AsyncTaskNewsParseJson().execute(urlPost);
ItemClickSupport itemClickSupport = ItemClickSupport.addTo(recyclerView);
itemClickSupport.setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position, long id) {
sharedPreferences.edit().putString("TITLE", designTitle[position]).apply();
sharedPreferences.edit().putString("CONTENT", designContent[position]).apply();
sharedPreferences.edit().putString("EXCERPT", designExcerpt[position]).apply();
sharedPreferences.edit().putString("IMAGE", designImageFull[position]).apply();
Intent intent = new Intent(getActivity(), DetailActivity.class);
startActivity(intent);
}
});
}
public class AsyncTaskNewsParseJson extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
// get JSON Object
@Override
protected String doInBackground(String... url) {
urlPost = url[0];
try {
jsonObjectDesignPosts = JsonParser.readJsonFromUrl(urlPost);
postNumber = jsonObjectDesignPosts.getJSONArray("posts").length();
jsonArrayDesignContent = jsonObjectDesignPosts.getJSONArray("posts");
sharedPreferences.edit().putString("DESIGN", jsonArrayDesignContent.toString()).apply();
designTitle = new String[postNumber];
designExcerpt = new String[postNumber];
designContent = new String[postNumber];
designImage = new String[postNumber];
designImageFull = new String[postNumber];
for (int i = 0; i < postNumber; i++) {
designTitle[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("posts").getJSONObject(i).getString("title")).toString();
designExcerpt[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("posts").getJSONObject(i).getString("excerpt")).toString();
designContent[i] = jsonObjectDesignPosts.getJSONArray("posts").getJSONObject(i).getString("content");
designImage[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("posts").getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("thumbnail").getString("url")).toString();
designImageFull[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("posts").getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("full").getString("url")).toString();
}
} catch (IOException | JSONException e) {
e.printStackTrace();
designTitle = new String[0];
error = true;
}
return null;
}
// Set facebook items to the textviews and imageviews
@Override
protected void onPostExecute(String result) {
designs = new ArrayList<>();
//Data set used by the recyclerViewAdapter. This data will be displayed.
if (designTitle.length != 0) {
for (int i = postNumber-1; i >= 0; i--) {
designs.add(new Design(designTitle[i], designExcerpt[i], designImage[i]));
}
}
if (error) {
Toast.makeText(getActivity(), "Houve um erro! Verifique se você está conectado na internet.", Toast.LENGTH_LONG).show();
}
// Create the recyclerViewAdapter
recyclerViewAdapter = new DesignAdapter(getActivity(), designs);
recyclerView.setAdapter(recyclerViewAdapter);
swipeRefreshLayout = (android.support.v4.widget.SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeRefreshLayout.setRefreshing(false);
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
}
}
private void swipeToRefresh(View view) {
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
int start = recyclerViewPaddingTop - convertToPx(48), end = recyclerViewPaddingTop + convertToPx(16);
swipeRefreshLayout.setProgressViewOffset(true, start, end);
TypedValue typedValueColorPrimary = new TypedValue();
TypedValue typedValueColorAccent = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimary, true);
getActivity().getTheme().resolveAttribute(R.attr.colorAccent, typedValueColorAccent, true);
final int colorPrimary = typedValueColorPrimary.data, colorAccent = typedValueColorAccent.data;
swipeRefreshLayout.setColorSchemeColors(colorPrimary, colorAccent);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new AsyncTaskNewsParseJson().execute(urlPost);
}
});
}
public int convertToPx(int dp) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (dp * scale + 0.5f);
}
public void toolbarHideShow() {
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.post(new Runnable() {
@Override
public void run() {
ScrollManagerToolbarTabs manager = new ScrollManagerToolbarTabs(getActivity());
manager.attach(recyclerView);
manager.addView(toolbar, ScrollManagerToolbarTabs.Direction.UP);
manager.setInitialOffset(toolbar.getHeight());
}
});
}
}
Is there any configuration on the server that is missing?
Thank you in advance!
Hello! What happens? Back in white? or not complete ? how does the designTitle in the onPostExecute???
– Thiago Luiz Domacoski
Thiago... whenever I put my URL in the app, it gives the error message "There was an error! Make sure you are connected on the internet." Whenever I change the URL to another alternative site that has Wordpress, it low normal.
– Will Cunha