Php 从sqlite检索数据,并尝试在android中使用json服务将其插入MySQL
我有一个数据库处理程序类,其中我有一个从sq lite数据库获取数据的函数。此代码在活动中完美运行,但在服务中未运行。此代码未给出任何错误Php 从sqlite检索数据,并尝试在android中使用json服务将其插入MySQL,php,android,mysql,sqlite,sync,Php,Android,Mysql,Sqlite,Sync,我有一个数据库处理程序类,其中我有一个从sq lite数据库获取数据的函数。此代码在活动中完美运行,但在服务中未运行。此代码未给出任何错误 package services; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import org.apache.http.NameValuePair; import org.apache.h
package services;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.varad.URL_OF_SERVER;
import com.example.varad.VaradSuccess;
import com.example.varad.lib.DatabaseHandler;
import com.example.varad.lib.JSONParser;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.example.varad.VaradSuccess;
public class Sync extends Service {
//strings
String id="";
String name="";
String contact="";
String occup="";
String org="";
String proptype="";
String propfur="";
String uptofloor="";
String loc="";
String pricefrom="";
String priceto="";
String property="";
String[][] aryDB = new String[5][12];
JSONObject json;
static URL_OF_SERVER os = new URL_OF_SERVER();
static String return_url = os.RETURN_URL();
private static String url_insert = return_url + "/sell.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
// Progress Dialog
private ProgressDialog pDialog;
int i=0,last_i=0,j=0;
JSONParser jsonParser = new JSONParser();
private static final String TAG = "Sync";
private static Timer timer = new Timer();
private Context ctx;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.i("Where in service","In oncreate startup");
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
Log.i("Where in service","after db object createdp");
j=0;
Log.i("Where in service","after j=0");
Cursor c=db.getData();
Log.i("Where in service","after Cursor c=db.getData();");
//Note:-This code run upto here. code not goes in if statement
if(c.moveToFirst()){
Log.i("Where in service","after if(c.moveToNext()){");
do{
Log.i("Where in service","after do{");
id=c.getString(c.getColumnIndex("id"));
name=c.getString(c.getColumnIndex("name"));
contact=c.getString(c.getColumnIndex("contact"));
occup=c.getString(c.getColumnIndex("occupation"));
org=c.getString(c.getColumnIndex("organization"));
proptype=c.getString(c.getColumnIndex("proptype"));
propfur=c.getString(c.getColumnIndex("propfur"));
uptofloor=c.getString(c.getColumnIndex("uptofloor"));
loc=c.getString(c.getColumnIndex("location"));
pricefrom=c.getString(c.getColumnIndex("pricefrom"));
priceto=c.getString(c.getColumnIndex("priceto"));
property=c.getString(c.getColumnIndex("property"));
aryDB[i][0]=id;
aryDB[i][1]=name;
aryDB[i][2]=contact;
aryDB[i][3]=occup;
aryDB[i][4]=org;
aryDB[i][5]=proptype;
aryDB[i][6]=propfur;
aryDB[i][7]=uptofloor;
aryDB[i][8]=loc;
aryDB[i][9]=pricefrom;
aryDB[i][10]=priceto;
aryDB[i][11]=property;
Toast.makeText(getApplicationContext(), aryDB[i][0]+" "+aryDB[i][1]+" "+aryDB[i][2]+" "+aryDB[i][3],Toast.LENGTH_SHORT).show();
Log.i("Data=", ""+i);
i++;
}while(c.moveToNext());
last_i=i;
new addCommsell().execute();
i=0;
db.close();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
class addCommsell extends AsyncTask<String, String, String> {
int success=0;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Sync.this);
pDialog.setMessage("Inserting Details..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Building Parameters
params.add(new BasicNameValuePair("name", aryDB[j][1]));
params.add(new BasicNameValuePair("contact", aryDB[j][2]));
params.add(new BasicNameValuePair("occupation", aryDB[j][3]));
params.add(new BasicNameValuePair("organization", aryDB[j][4]));
params.add(new BasicNameValuePair("proptype", aryDB[j][5]));
params.add(new BasicNameValuePair("propfur", aryDB[j][6]));
params.add(new BasicNameValuePair("uptofloor", aryDB[j][7]));
params.add(new BasicNameValuePair("location", aryDB[j][8]));
params.add(new BasicNameValuePair("pricefrom", aryDB[j][9]));
params.add(new BasicNameValuePair("priceto", aryDB[j][10]));
params.add(new BasicNameValuePair("property", aryDB[j][11]));
json = jsonParser.makeHttpRequest(url_insert, "POST",
params);
// getting JSON Object
// Note that create product url accepts POST method
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
}
else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
j++;
if(j!=last_i){
new addCommsell().execute();
}
Toast.makeText(Sync.this, " s="+success+" j= "+ j +" last_i = "+last_i, Toast.LENGTH_SHORT).show();
Log.i("posation of post "," s="+success+" j= "+ j +" last_i = "+last_i );
return ;
}
}
}
套餐服务;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Timer;
导入java.util.TimerTask;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入\u服务器的com.example.varad.URL\u;
导入com.example.varad.VaradSuccess;
导入com.example.varad.lib.DatabaseHandler;
导入com.example.varad.lib.JSONParser;
导入android.app.ProgressDialog;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.os.AsyncTask;
导入android.os.Handler;
导入android.os.IBinder;
导入android.os.Message;
导入android.util.Log;
导入android.widget.Toast;
导入com.example.varad.VaradSuccess;
公共类同步扩展服务{
//弦
字符串id=“”;
字符串名称=”;
字符串联系人=”;
字符串占用=”;
字符串org=“”;
字符串类型=”;
字符串propfur=“”;
字符串“地板=”“;
字符串loc=”“;
字符串pricefrom=“”;
字符串priceto=“”;
字符串属性=”;
字符串[][]aryDB=新字符串[5][12];
JSONObject json;
_服务器os的静态URL_=_服务器()的新URL_;
静态字符串return_url=os.return_url();
私有静态字符串url\u insert=return\u url+“/sell.php”;
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
//进度对话框
私人对话;
int i=0,last_i=0,j=0;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串TAG=“Sync”;
专用静态计时器=新计时器();
私有上下文ctx;
@凌驾
公共IBinder onBind(意图arg0){
//TODO自动生成的方法存根
返回null;
}
public void onCreate(){
Toast.makeText(这是“我的服务已创建”,Toast.LENGTH_LONG.show();
Log.i(“在服务中的位置”、“在oncreate启动中”);
DatabaseHandler db=新的DatabaseHandler(getApplicationContext());
Log.i(“在服务中的位置”,“在db对象createdp之后”);
j=0;
Log.i(“在使用中”、“j=0后”);
游标c=db.getData();
Log.i(“在服务中的位置”,“光标c=db.getData();”之后);
//注意:-此代码在此处运行。代码不在if语句中
if(c.moveToFirst()){
Log.i(“在服务中的位置”,“在if(c.moveToNext()){”之后);
做{
Log.i(“在服务的地方”,“在do{”)之后;
id=c.getString(c.getColumnIndex(“id”);
name=c.getString(c.getColumnIndex(“name”);
contact=c.getString(c.getColumnIndex(“contact”);
occup=c.getString(c.getColumnIndex(“职业”);
org=c.getString(c.getColumnIndex(“组织”);
proptype=c.getString(c.getColumnIndex(“proptype”);
propfur=c.getString(c.getColumnIndex(“propfur”);
uptofloor=c.getString(c.getColumnIndex(“uptofloor”);
loc=c.getString(c.getColumnIndex(“位置”);
pricefrom=c.getString(c.getColumnIndex(“pricefrom”);
priceto=c.getString(c.getColumnIndex(“priceto”);
property=c.getString(c.getColumnIndex(“property”);
aryDB[i][0]=id;
aryDB[i][1]=名称;
aryDB[i][2]=联系人;
aryDB[i][3]=占位;
aryDB[i][4]=组织;
aryDB[i][5]=固有型;
aryDB[i][6]=原纤维;
aryDB[i][7]=住宅楼;
aryDB[i][8]=loc;
aryDB[i][9]=价格来源;
aryDB[i][10]=价格;
aryDB[i][11]=不动产;
Toast.makeText(getApplicationContext(),aryDB[i][0]+“”+aryDB[i][1]+“”+aryDB[i][2]+“”+aryDB[i][3],Toast.LENGTH_SHORT).show();
Log.i(“数据=”,“”+i);
i++;
}而(c.moveToNext());
last_i=i;
新建addCommsell().execute();
i=0;
db.close();
}
}
@凌驾
公共空间{
super.ondestory();
Toast.makeText(此“服务已停止…”,Toast.LENGTH_SHORT.show();
}
类addCommsell扩展了异步任务{
int成功=0;
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(Sync.this);
pDialog.setMessage(“插入详细信息…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*创造产品
* */
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
//建筑参数
参数添加(新的BasicNameValuePair(“名称”,aryDB[j][1]);
参数添加(新的BasicNameValuePair(“联系人”,aryDB[j][2]);
新增参数(新的BasicNameValuePair(“职业”,aryDB[j][3]);
参数add(新的BasicNameValuePair(“组织”,aryDB[j][4]);
参数添加(新的BasicNameValuePair(“proptype”,aryDB[j][5]);
参数添加(新的BasicNameValuePair(“propfur”,aryDB[j][6]);
参数添加(新的BasicNameValuePair(“uptofloor”,aryDB[j][7]);
参数add(新的BasicNameValuePair(“位置”,aryDB[j][8]);
参数添加(新的BasicNameValuePair(“pricefrom”,aryDB[j][9]);
参数添加(新的BasicNameValuePair(“priceto”,aryDB[j][10]);
参数添加(新的BasicNameValuePair(“属性”),ary
package services;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.varad.URL_OF_SERVER;
import com.example.varad.VaradSuccess;
import com.example.varad.lib.DatabaseHandler;
import com.example.varad.lib.JSONParser;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import com.example.varad.VaradSuccess;
public class Sync extends Service {
//strings
String id="";
String name="";
String contact="";
String occup="";
String org="";
String proptype="";
String propfur="";
String uptofloor="";
String loc="";
String pricefrom="";
String priceto="";
String property="";
String[][] aryDB = new String[5][12];
JSONObject json;
static URL_OF_SERVER os = new URL_OF_SERVER();
static String return_url = os.RETURN_URL();
private static String url_insert = return_url + "/sell.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
// Progress Dialog
//private ProgressDialog pDialog;
int i=0,last_i=0,j=0;
JSONParser jsonParser = new JSONParser();
private static final String TAG = "Sync";
private static Timer timer = new Timer();
private Context ctx;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.i("Where in service","In oncreate startup");
DatabaseHandler db=new DatabaseHandler(getApplicationContext());
Log.i("Where in service","after db object createdp");
j=0;
Log.i("Where in service","after j=0");
Cursor c=db.getData();
Log.i("Where in service","after Cursor c=db.getData();");
//Note:-This code run upto here. code not goes in if statement
if(c.moveToFirst()){
Log.i("Where in service","after if(c.moveToNext()){");
do{
Log.i("Where in service","after do{");
id=c.getString(c.getColumnIndex("id"));
name=c.getString(c.getColumnIndex("name"));
contact=c.getString(c.getColumnIndex("contact"));
occup=c.getString(c.getColumnIndex("occupation"));
org=c.getString(c.getColumnIndex("organization"));
proptype=c.getString(c.getColumnIndex("proptype"));
propfur=c.getString(c.getColumnIndex("propfur"));
uptofloor=c.getString(c.getColumnIndex("uptofloor"));
loc=c.getString(c.getColumnIndex("location"));
pricefrom=c.getString(c.getColumnIndex("pricefrom"));
priceto=c.getString(c.getColumnIndex("priceto"));
property=c.getString(c.getColumnIndex("property"));
aryDB[i][0]=id;
aryDB[i][1]=name;
aryDB[i][2]=contact;
aryDB[i][3]=occup;
aryDB[i][4]=org;
aryDB[i][5]=proptype;
aryDB[i][6]=propfur;
aryDB[i][7]=uptofloor;
aryDB[i][8]=loc;
aryDB[i][9]=pricefrom;
aryDB[i][10]=priceto;
aryDB[i][11]=property;
Toast.makeText(getApplicationContext(), aryDB[i][0]+" "+aryDB[i][1]+" "+aryDB[i][2]+" "+aryDB[i][3],Toast.LENGTH_SHORT).show();
Log.i("Data=", ""+i);
i++;
}while(c.moveToNext());
last_i=i;
new addCommsell().execute();
i=0;
db.close();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
class addCommsell extends AsyncTask<String, String, String> {
int success=0;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
/*
pDialog = new ProgressDialog(Sync.this);
pDialog.setMessage("Inserting Details..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
*/
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Building Parameters
params.add(new BasicNameValuePair("name", aryDB[j][1]));
params.add(new BasicNameValuePair("contact", aryDB[j][2]));
params.add(new BasicNameValuePair("occupation", aryDB[j][3]));
params.add(new BasicNameValuePair("organization", aryDB[j][4]));
params.add(new BasicNameValuePair("proptype", aryDB[j][5]));
params.add(new BasicNameValuePair("propfur", aryDB[j][6]));
params.add(new BasicNameValuePair("uptofloor", aryDB[j][7]));
params.add(new BasicNameValuePair("location", aryDB[j][8]));
params.add(new BasicNameValuePair("pricefrom", aryDB[j][9]));
params.add(new BasicNameValuePair("priceto", aryDB[j][10]));
params.add(new BasicNameValuePair("property", aryDB[j][11]));
json = jsonParser.makeHttpRequest(url_insert, "POST",
params);
// getting JSON Object
// Note that create product url accepts POST method
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
}
else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
//pDialog.dismiss();
j++;
if(j!=last_i){
new addCommsell().execute();
}
Toast.makeText(Sync.this, " s="+success+" j= "+ j +" last_i = "+last_i, Toast.LENGTH_SHORT).show();
Log.i("posation of post "," s="+success+" j= "+ j +" last_i = "+last_i );
return ;
}
}
}