I'm trying to get blogger information via json
and put it to appear in recyclerview
.
But I can not. I'm a beginner in programming, so I'm unsure if you'll have to sign some key
so you can release content by json
.
Edit: Data is not being displayed.
Edit 2: It seems like finding out what the problem is, but I still can not solve it. It turns out that what I want to get is not a JSONArray:
JSONObject response = new JSONObject(result);
JSONArray version = response.optJSONArray("title");
And yes a JSONObject:
JSONObject response = new JSONObject(result);
JSONObject getTitle = response.getJSONObject("title");
Until then the app persisted in Fault Toast, after that change the message no longer appears. However, nothing on the screen is still displayed.
This appears when I'm monitoring app progress: W / System.err: org.json.JSONException: No value for title
Since title is really an object.
Code after change:
private void parseResult(String result) {
Log.d("Debug", result);
try {
JSONObject response = new JSONObject(result);
JSONObject getTitle = response.getJSONObject("title");
rowItems = new ArrayList<>();
for (int i = 0; i < getTitle.length(); i++) {
RowItem item = new RowItem();
String put = getTitle.getString("$t");
item.setTitle(put);
rowItems.add(item);
}
} catch (JSONException e){
e.printStackTrace();
}
}
}
Here is the java code for my application.
RowItem:
public class RowItem {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Adapter:
public class IAdapter extends RecyclerView.Adapter<IAdapter.CustomViewHolder> {
private List<RowItem> listItems;
private Context mContext;
private OnItemClickListener onItemClickListener;
public IAdapter(Context context, List<RowItem> listItems) {
this.listItems = listItems;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listar_apps, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
final RowItem listItem = listItems.get(i);
customViewHolder.applyTitle.setText(Html.fromHtml(listItem.getTitle()));
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(listItem);
}
};
customViewHolder.applyTitle.setOnClickListener(listener);
}
@Override
public int getItemCount() {
return (null != listItems ?
listItems.size() : 0);
}
class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView applyTitle;
public CustomViewHolder(View view) {
super(view);
this.applyTitle = (TextView) view.findViewById(R.id.titulo_app);
}
}
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}
Main activity:
public class Creator extends AppCompatActivity {
private RecyclerView recyclerView;
private static final String TAG = "Creator";
private List<RowItem> rowItems;
private IAdapter adapter;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creator);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String url = "http://techpositivo.blogspot.com/feeds/posts/default?alt=json";
new GetData().execute(url);
}
public class GetData extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
HttpURLConnection urlConnection;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
int statusCode = urlConnection.getResponseCode();
//200 representa HTTP OK
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseResult(response.toString());
result = 1; //Sucesso
} else {
result = 0; //Falhou
}
} catch (Exception e){
Log.d(TAG, e.getLocalizedMessage());
}
return result; //Falhou
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
adapter = new IAdapter(Creator.this, rowItems);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(RowItem item) {
Toast.makeText(Creator.this, item.getTitle(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(Creator.this, "Falhou...", Toast.LENGTH_SHORT).show();
}
}
}
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray version = response.optJSONArray("title");
rowItems = new ArrayList<>();
for (int i = 0; i < version.length(); i++) {
JSONObject title = version.optJSONObject(i);
RowItem item = new RowItem();
item.setTitle(title.optString("$t"));
rowItems.add(item);
}
} catch (JSONException e){
e.printStackTrace();
}
}
}
Note: Do not notice my site in the desktop version, because my intention is only for smartphones.