I am using webservices in my app. Basically my app shows a posts of wordpress site. User can also bookmark the posts.I am saving all book mark posts in sqlite with post urls. Now the problem is I have many different urls. And i want to show the content of these url in One ListView
.
Also json structure of all these urls is same.
I have looked upon other question related to this issue but those aren’t helping much.
Let me show you how much i have tried so far.
here is code
this is code of BookmarkActivity
ArrayList<HashMap<String,String>> allData;
String[] urlarray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmark);
SqliteController controller= new SqliteController(this);
allData = controller.getAllData();
for (int i=0; i<allData.size(); i++){
String url=allData.get(i).get("link");
urlarray= new String[]{url};
}
for(int i=0; i <urlarray.length; i++){
MyAsyncTask task = new MyAsyncTask(i);
task.execute();
}
}
private class MyAsyncTask extends AsyncTask<Object,Void,String>{
int urlNumber;
HttpURLConnection connection=null;
BufferedReader reader=null;
public MyAsyncTask (int number){
this.urlNumber=number;
}
@Override
protected String doInBackground(Object... params) {
try {
URL url = new URL(urlarray[urlNumber]);
connection= (HttpURLConnection) url.openConnection();
connection.connect();
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String json = buffer.toString();
return json;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Log.d("TAG",s);
}
}
here is json. (its not the exact same json but i am sure every url will have same json structure)
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{
"id": 1,
"type": "post",
"slug": "hello-world",
"url": "http://localhost/wordpress/?p=1",
"title": "Hello world!",
"title_plain": "Hello world!",
"content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</p>n",
"excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!n",
"date": "2009-11-11 12:50:19",
"modified": "2009-11-11 12:50:19",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "",
"url": "",
"description": ""
},
"comments": [
{
"id": 1,
"name": "Mr WordPress",
"url": "http://wordpress.org/",
"date": "2009-11-11 12:50:19",
"content": "<p>Hi, this is a comment.<br />To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.</p>n",
"parent": 0
}
],
"comment_count": 1,
"comment_status": "open"
}
]
}
Can somebody help me with this. How should i work around with such situation?
try this one…Actually in this code am using Volley Library for Network Operations.
First of all create Application class in your Application to avoid creating new instance of Volley library for network operations.
In app build.gradle add this line under dependencies
In manifest file add this permission to access network
in Application tag mentioned the application class name
In Activity onCreate add this line…
You should be doing every execution in the onBackground method after you get the result of one asynchronous call. This is because the async task executes on a different thread.