Android app met data van site
Ik ben bezig met een app/site, het is de bedoeling dat hij informatie van mijn site haalt:
Ik heb een lijst op mijn site die je kan invullen en vervolgens op die app ziet.
Dus mijn site is zeg maar de kern van de app.
Maar wat is nou echt handig om te doen alles in mysql? Is dat dan wel handig met het programmeren van de app, wat overigens in java gebeurd? Wat raden jullie aan?
Alvast bedankt
Gewijzigd op 09/03/2013 22:36:55 door Keizer Webdesign
Maak dus gewoon een PHP API met als output XML of JSON en gebruik dat in je app. Als je iets wilt kunnen toevoegen moet je ook gewoon een request doen naar die API, bijvoorbeeld POST http://example.com/api/add (simplistisch voorbeeld, je kan ook een JSON request body doorgeven en daaruit de actie halen).
Ik heb namelijk een hele library nodig om XML te parse...
Dus wat is dan een goed te parse, snel en makkelijk bestandtype?
Dit gebruik ik nu voor een van mijn apps (met een Fragment en AsyncHTTPClient van loopj.com):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.koenv.pontes;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.loopj.android.http.*;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class LerarenFragment extends ListFragment {
/**
* Constants for the XML file
* Format of XML file:
* <pre>
* <api>
* <leraren>
* <leraar>
* <afkorting>AC</afkorting>
* <naam>M. Amat</naam>
* <voornaam>Marianne</voornaam>
* <tussenvoegsel></tussenvoegsel>
* <achternaam>Amat</achternaam>
* </leraar>
* </leraren>
* </api>
* </pre>
*/
static final String KEY_LERAAR = "leraar"; // parent node
static final String KEY_NAAM = "naam";
static final String KEY_AFKORTING = "afkorting";
static final String KEY_VOORNAAM = "voornaam";
static final String KEY_TUSSENVOEGSEL = "tussenvoegsel";
static final String KEY_ACHTERNAAM = "achternaam";
/**
* Our result
*/
private String _result = null;
/**
* The adapter for the view
*/
private SimpleAdapter mAdapter;
private Handler mHandler;
/**
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View V = inflater.inflate(R.layout.tab_frag1_layout, container, false);
mHandler = new Handler();
return V;
}
/**
* @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
*/
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//addSearch();
if (_result != null) {
doLerarenList();
} else {
RequestParams params = new RequestParams();
params.put("action", "getLeraren");
params.put("APIKey", "ExampleAPI");
HetGoeseLyceumRestClient.post("http://example.com", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
doLerarenList(response);
}
@Override
public void onStart() {
getActivity().setProgressBarIndeterminateVisibility(true);
}
@Override
public void onFinish() {
getActivity().setProgressBarIndeterminateVisibility(false);
}
@Override
public void onFailure(Throwable error) {
Toast.makeText(getActivity(), getString(R.string.fout) + error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
/**
* @see android.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
HeaderViewListAdapter adapter = ((HeaderViewListAdapter) getListView().getAdapter());
@SuppressWarnings("unchecked")
HashMap<String, String> map = (HashMap<String, String>) adapter.getItem(position);
Leraar leraar = new Leraar(map);
Intent intent = new Intent(getActivity(), LerarenDetail.class);
intent.putExtra(getString(R.string.leraar_descriptor), leraar);
startActivity(intent);
}
/**
* Put items in our {android.widget.ListView}
* @param response The XML file structure
*/
protected void doLerarenList(String response) {
this._result = response;
doLerarenList();
}
/**
* Filter the {android.widget.ListView}
* @param s On what to filter
*/
protected void filter(String s) {
if (getListView().getAdapter() != null) {
getListView().setTextFilterEnabled(true);
((HeaderViewListAdapter) getListView().getAdapter()).getFilter().filter(s);
}
}
/**
* Add filterbox to {android.widget.ListView}
*/
protected void addSearch() {
//if (mAdapterSet == true) {
//m
setListAdapter(null);
//return;
//}
EditText et = new EditText(getActivity());
et.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
filter(s.toString());
}
});
et.setHint(R.string.leraren);
getListView().addHeaderView(et);
}
public void doLerarenList() {
// Do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
addSearch();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(_result);
NodeList nl = doc.getElementsByTagName(KEY_LERAAR);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// looping through all item nodes <leraar>
HashMap<String, String> map = new HashMap<String, String>();
Element e;
for (int i = 0; i < nl.getLength(); i++) {
map = new HashMap<String, String>();
e = (Element) nl.item(i);
map.put(KEY_NAAM, parser.getValue(e, KEY_NAAM));
map.put(KEY_AFKORTING, parser.getValue(e, KEY_AFKORTING));
map.put(KEY_VOORNAAM, parser.getValue(e, KEY_VOORNAAM));
map.put(KEY_TUSSENVOEGSEL, parser.getValue(e, KEY_TUSSENVOEGSEL));
map.put(KEY_ACHTERNAAM, parser.getValue(e, KEY_ACHTERNAAM));
menuItems.add(map);
}
mAdapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.leraren_row, new String[] { KEY_AFKORTING, KEY_NAAM },
new int[] { R.id.afkorting, R.id.naam });
setListAdapter(mAdapter);
}
};
mHandler.post(runnable);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.loopj.android.http.*;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class LerarenFragment extends ListFragment {
/**
* Constants for the XML file
* Format of XML file:
* <pre>
* <api>
* <leraren>
* <leraar>
* <afkorting>AC</afkorting>
* <naam>M. Amat</naam>
* <voornaam>Marianne</voornaam>
* <tussenvoegsel></tussenvoegsel>
* <achternaam>Amat</achternaam>
* </leraar>
* </leraren>
* </api>
* </pre>
*/
static final String KEY_LERAAR = "leraar"; // parent node
static final String KEY_NAAM = "naam";
static final String KEY_AFKORTING = "afkorting";
static final String KEY_VOORNAAM = "voornaam";
static final String KEY_TUSSENVOEGSEL = "tussenvoegsel";
static final String KEY_ACHTERNAAM = "achternaam";
/**
* Our result
*/
private String _result = null;
/**
* The adapter for the view
*/
private SimpleAdapter mAdapter;
private Handler mHandler;
/**
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View V = inflater.inflate(R.layout.tab_frag1_layout, container, false);
mHandler = new Handler();
return V;
}
/**
* @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
*/
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//addSearch();
if (_result != null) {
doLerarenList();
} else {
RequestParams params = new RequestParams();
params.put("action", "getLeraren");
params.put("APIKey", "ExampleAPI");
HetGoeseLyceumRestClient.post("http://example.com", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
doLerarenList(response);
}
@Override
public void onStart() {
getActivity().setProgressBarIndeterminateVisibility(true);
}
@Override
public void onFinish() {
getActivity().setProgressBarIndeterminateVisibility(false);
}
@Override
public void onFailure(Throwable error) {
Toast.makeText(getActivity(), getString(R.string.fout) + error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
/**
* @see android.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
HeaderViewListAdapter adapter = ((HeaderViewListAdapter) getListView().getAdapter());
@SuppressWarnings("unchecked")
HashMap<String, String> map = (HashMap<String, String>) adapter.getItem(position);
Leraar leraar = new Leraar(map);
Intent intent = new Intent(getActivity(), LerarenDetail.class);
intent.putExtra(getString(R.string.leraar_descriptor), leraar);
startActivity(intent);
}
/**
* Put items in our {android.widget.ListView}
* @param response The XML file structure
*/
protected void doLerarenList(String response) {
this._result = response;
doLerarenList();
}
/**
* Filter the {android.widget.ListView}
* @param s On what to filter
*/
protected void filter(String s) {
if (getListView().getAdapter() != null) {
getListView().setTextFilterEnabled(true);
((HeaderViewListAdapter) getListView().getAdapter()).getFilter().filter(s);
}
}
/**
* Add filterbox to {android.widget.ListView}
*/
protected void addSearch() {
//if (mAdapterSet == true) {
//m
setListAdapter(null);
//return;
//}
EditText et = new EditText(getActivity());
et.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
filter(s.toString());
}
});
et.setHint(R.string.leraren);
getListView().addHeaderView(et);
}
public void doLerarenList() {
// Do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
addSearch();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(_result);
NodeList nl = doc.getElementsByTagName(KEY_LERAAR);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// looping through all item nodes <leraar>
HashMap<String, String> map = new HashMap<String, String>();
Element e;
for (int i = 0; i < nl.getLength(); i++) {
map = new HashMap<String, String>();
e = (Element) nl.item(i);
map.put(KEY_NAAM, parser.getValue(e, KEY_NAAM));
map.put(KEY_AFKORTING, parser.getValue(e, KEY_AFKORTING));
map.put(KEY_VOORNAAM, parser.getValue(e, KEY_VOORNAAM));
map.put(KEY_TUSSENVOEGSEL, parser.getValue(e, KEY_TUSSENVOEGSEL));
map.put(KEY_ACHTERNAAM, parser.getValue(e, KEY_ACHTERNAAM));
menuItems.add(map);
}
mAdapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.leraren_row, new String[] { KEY_AFKORTING, KEY_NAAM },
new int[] { R.id.afkorting, R.id.naam });
setListAdapter(mAdapter);
}
};
mHandler.post(runnable);
}
}
Zie dit voor een JSON voorbeeld.
Gewijzigd op 10/03/2013 16:22:33 door Koen Vlaswinkel