Android 中的电子商务
我是android开发的新手,现在我正在android中开发一个用于在线购物的电子商务应用程序。到目前为止,在这个项目中我已经创建了一个购物车来保存用户选择的文章。但我所做的每一件事都是静态的。
现在我必须与服务器通信以获取有关产品和折扣的更新。
我知道如何使用 json 来使用 Web 服务。
1.我很困惑,每次启动应用程序时,我都应该使用网络服务与服务器通信?或者当应用程序在设备上第一次执行时我应该将数据存储在应用程序中。
2.我应该遵循什么方法来存储数据
我应该将其存储在设备缓存中或 我应该将它存储在应用程序 sqlite db 中
编辑: 从网络服务获取数据
try {
Log.v("inside webservice call ", "1");
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
Log.v("inside webservice call ", "2");
JSONStringer JsonString = new JSONStringer().object().key("prodid")
.value("4057339").endObject();
StringEntity entity = new StringEntity(JsonString.toString());
request.setEntity(entity);
Log.v("data", JsonString.toString());
Log.v("inside webservice call ", "2");
DefaultHttpClient httpClient1 = new DefaultHttpClient();
HttpResponse response = httpClient1.execute(request);
Log.v("inside webservice call ", "3");
Log.v("response code", response.getStatusLine().getStatusCode()
+ "");
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int) responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
Log.v("inside webservice call ", "4");
results = new JSONObject(new String(buffer));
// Populate text fields
String message = results.getString("message");
String isvalid = results.getString("isvalid");
Log.v("tag", message + isvalid);
Log.v("inside webservice call ", "5");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
我这样做是为了从服务器获取数据
,但无法将此数据放入 sqlite 数据库中,请指导我
I am new to android development and now i am developing an e commerce application in android for online shopping.So far in this project i have created a shopping cart to hold articles selected by user. But every thing i have done works statically.
Now i have to communicate with the server to get updates about products and discounts.
I know how to consume webservices using json.
1.I am confused about that everytime i start my application i should communicate with server using webservice ?? or should i store the data in application when application is executed for the first time on device.
2.And what approach to store data should i follow
I should store it in device cache or
I should store it in applications sqlite db
Edit :
get data from webservice
try {
Log.v("inside webservice call ", "1");
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
Log.v("inside webservice call ", "2");
JSONStringer JsonString = new JSONStringer().object().key("prodid")
.value("4057339").endObject();
StringEntity entity = new StringEntity(JsonString.toString());
request.setEntity(entity);
Log.v("data", JsonString.toString());
Log.v("inside webservice call ", "2");
DefaultHttpClient httpClient1 = new DefaultHttpClient();
HttpResponse response = httpClient1.execute(request);
Log.v("inside webservice call ", "3");
Log.v("response code", response.getStatusLine().getStatusCode()
+ "");
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int) responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
Log.v("inside webservice call ", "4");
results = new JSONObject(new String(buffer));
// Populate text fields
String message = results.getString("message");
String isvalid = results.getString("isvalid");
Log.v("tag", message + isvalid);
Log.v("inside webservice call ", "5");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
I do this to get data from server
but unable to put this data in sqlite db please guide me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这完全取决于您对项目的要求,意味着如果数据要频繁更新,那么您应该每次都加载它,您也可以定期加载数据,如果可能的话,从服务器提供一项额外的服务来通知您更新,您只需只需每次都检查一下。
对于图像和其他媒体,您可以将它们存储到 SDCard 一次,下次只需检查它们是否已经存在,然后不要加载,这可以节省您的处理时间。
要在加载许多特定的、格式化的和分类的信息时存储数据,那么 sqlite db 将更有助于存储和检索数据。
This totally depends on your requirement of the project,means if the data is going to be updated frequently then you should load it everytime,you can also load data periodically and if possible make one extra service from server that notify you for updates and you just need to check that only everytime.
For the images and other media you can store them to SDCard once and next time just check if they alreay exist then don't go for loading,this can save your process time.
To store the data as you are loading many specific and formatted and categoriesed information then sqlite db would be more help ful to store and retrieve data.
取决于您的应用程序的架构。如果此数据没有更改,那么您可以在首次启动时下载它,然后重用它。
设备缓存?据我所知没有这样的事情。通常复杂的数据存储在数据库中。但这实际上取决于数据的类型和您的访问要求(搜索等)。查看Android 上的所有数据存储选项。
Depends on the architecture of your app. If this data does not change, then you just might download it on first start and then reuse it.
Device cache? I know of no such thing. Normally complex data is stored in database. But this really depends on the type of data and access requirements you have (searching, etc..). See all data storage options on Android.
根据您存储的数据类型,我会使用其中之一。为了缓存二进制数据,我使用文件系统(如您所说的“设备缓存”),但对于更复杂的结构,我更喜欢使用 SQLite DB。 SQLite 的一个优点是,您可以使用众所周知的 SQL 查询轻松地在每次更新时更改结构。
哦@Frankenstein,比我快:D
编辑:如果您计划缓存类别和产品集合,我将始终使用 SQLite DB!
如果媒体文件是静态的,您甚至可以将它们与应用程序资源一起发送。以防它们永远不会改变。
Depending on the kind of data your storing, I would use one or the other. For caching binary data I use the file system ("device cache" as you say), but for more complex structures I prefer to use SQLite DB. An advantage of SQLite is, that you can alter your structure with every update easily using well known SQL Queries.
Oh @Frankenstein, was faster than me :D
EDIT: If you plan to cache categories and product collections I would always use a SQLite DB!
If the media files are static, you can even ship them with your application resources. In case they are never changing.