**i am devloping a web application in android... i have a database helper class which store sqlite data... my api to fetch json data is http://ift.tt/1GOKrSE
i want to save json data to sqlite as well as json .. i m little bit confused of use of web service.. i want a application which fetch online data from application and save data online ..but i don't want to use mysql-php i need a sqlite database which store online data My add contact activity for this****
DatabaseHelper dbHelper=new DatabaseHelper(this);
private static String url = "http://ift.tt/1GOKuxR /contacts_jsons";
private ProgressDialog pDialog;
when i click on button it call asynck task
ADD=(Button)findViewById(R.id.button_add);
ADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sname=Name.getText().toString();
semail=Email.getText().toString();
new saveContacts().execute();
}
});
**Save contact asynck task which calls json api to save in sqlite **
private class saveContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Add_contact.this);
pDialog.setMessage("Please wait Saving...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("code","A"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONObject parentObject = new JSONObject(result);
JSONObject userDetails = parentObject.getJSONObject("user_details");
dbHelper.Add_Record(userDetails.getString("user_name"),
userDetails.getString("user_email"));
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
**//My Database helper class for this which store my json data **
public void Add_Record(String email, String name) {
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(KEY_EMAIL,email);
cv.put(KEY_NAME, name);
db.insert(TABLE_Record, null, cv);
}
**I want to store my sqlite data into json ..means when i click on button my data goes to my json api then i fetch from json to sqlite **
Aucun commentaire:
Enregistrer un commentaire