Created
August 22, 2017 23:21
-
-
Save AladinDridi/dad734a41791393ea9019e5f8210089a to your computer and use it in GitHub Desktop.
json array -rest api wordpress for android
This file contains hidden or 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
package com.example.aladin_bureau.wordpress_wp; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.ListView; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import adapters.postadapter; | |
import entities.*; | |
public class MainActivity extends AppCompatActivity implements onFeedListener { | |
ListView list ; | |
ArrayList<posts>postses; | |
postadapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
list=(ListView)findViewById(R.id.list1); | |
adapter= new postadapter(getApplicationContext(),postses); | |
list.setAdapter(adapter); | |
FeedTask task = new FeedTask(this); | |
task.execute("https://blog.rumi-app.org/wp-json/wp/v2/posts"); | |
} | |
@Override | |
public void onFeed(JSONArray array) { | |
int length = array.length(); | |
postses=new ArrayList<>(); | |
for (int i=0 ; i< length;i++){ | |
JSONObject object = array.optJSONObject(i); | |
posts post = new posts(); | |
post.setDescription(object.optString("title")); | |
post.setDescription(object.optString("excerpt")); | |
post.setDescription(""); | |
postses.add(post); | |
} | |
adapter.addAll(postses); | |
adapter.notifyDataSetChanged(); | |
} | |
public class FeedTask extends AsyncTask<String,Void,JSONArray>{ | |
private onFeedListener listener ; | |
public FeedTask (onFeedListener listener){ | |
this.listener=listener; | |
} | |
@Override | |
protected JSONArray doInBackground(String... params) { | |
String url = params[0]; | |
OkHttpClient client = new OkHttpClient(); | |
Request.Builder builder = new Request.Builder(); | |
Request request=builder.url(url).build(); | |
try { | |
Response response = client.newCall(request).execute(); | |
String json = response.body().string(); | |
JSONObject object = new JSONObject(); | |
JSONArray array = object.optJSONArray("posts"); | |
return array; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(JSONArray array) { | |
super.onPostExecute(array); | |
if (null==array){ | |
return; | |
} else if(null!=listener){ | |
listener.onFeed(array); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment