好的,我做的可能不是很干净,但它有效
我在循环中从光标获取数据,每次调用上传函数时
从游标数据库获取数据 db = new Database(this); 光标 c = db.getSpolecnostiUpSyn();
int sp_id;
String sp_spolecnost;
if (c != null ) {
if (c.moveToFirst()) {
do {
sp_id = c.getInt(c.getColumnIndex(Database.COLUMN_ID_SPOLECNOST));
sp_spolecnost = c.getString(c.getColumnIndex(Database.COLUMN_SPOLECNOST_SPOLECNOST));
spolecnostNahraj(sp_id, sp_spolecnost); // for upload data each row
Log.d("up spolecnost", "nahrano");
}while (c.moveToNext());
}
}
}
并上传
void spolecnostNahraj(int idspolecnosti, String spolecnost){
JSONArray jArray;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
nameValuePairs.add(new BasicNameValuePair("spolecnost",spolecnost));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***.cz/syn/****.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
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,"UTF-8"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
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());
}
//paring data
int sp_id;
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
Database db = new Database(this);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
sp_id=json_data.getInt("ID_Spol_Pumpa");
db.opravOdSpolecnosti(idspolecnosti, sp_id);
Log.d("id spolecnosti vlozenyho", String.valueOf(sp_id));
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
我希望能帮助别人