我想将post
数据库记录发送到网络服务器:
public ArrayList<String> getAllNewOrUpdatedParcelles() {
ArrayList<String> ret = new ArrayList<String>();
String enreg, quartier_id = getCommune().getQuartierId();
String query = "SELECT * FROM " + T_PARCELLE + " WHERE bien_code != 0 AND ( updated = 'true' OR new_enreg = 'true')";
Cursor c = bd.rawQuery(query, null);
if (c != null && c.moveToFirst()) {
do {
enreg = "data_parcelle=yes";
enreg += "&bien_code="+c.getString(0);
enreg += "&decoup_terri_code="+c.getString(1);
enreg += "&dec_decoup_terri_code="+c.getString(2);
enreg += "&bien_ident="+c.getString(3);
enreg += "&parcel_denomination="+c.getString(4);
enreg += "&parcel_porte_ppale="+c.getString(5);
enreg += "&parcel_porte_second="+c.getString(6);
enreg += "&parcel_superfi_totale="+c.getString(7);
enreg += "&parcel_superf_batie="+c.getString(8);
enreg += "&parcel_superf_non_batie="+c.getString(9);
enreg += "&parcel_superf_plani="+c.getString(10);
enreg += "&quartier_code="+quartier_id;
enreg += "&parcel_date_deb_construct="+c.getString(12);
enreg += "&parcel_date_fin_construct="+c.getString(13);
if (c.getString(14).equalsIgnoreCase("true"))
enreg += "&nouvel_enreg=yes";
else
enreg += "&nouvel_enreg=no";
ret.add(enreg);
} while (c.moveToNext());
}
return ret;
}
http帖子:
private void postData(String p_url) {
HttpURLConnection urlConnection = null;
OutputStreamWriter writer = null;
try {
ArrayList<String> ret = db.getAllNewOrUpdatedParcelles();
if (ret.size() > 0) {
for (String enreg : ret) {
URL url = new URL(p_url);
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(enreg);
writer.flush();
writer.close();
urlConnection.disconnect();
}
}
} catch (MalformedURLException e) {
error = true;
err = contextInsideDialogue.getResources().getString(R.string.errBadUrl);
} catch (IOException e) {
error = true;
err = contextInsideDialogue.getResources().getString(R.string.errAccessError);
} finally {
if (writer != null) { try { writer.close(); } catch (IOException e) {} }
if (urlConnection != null) { urlConnection.disconnect(); }
}
}
更新:
在服务器端(PHP),我将这样的帖子数据视为:
$db = new DbConn() ;
$trace = new trace($db->getInstance(), T_TRACE_ACCES);
/*
*
* traitement des parcelles
*
*/
if (isset($_POST['data_parcelle'])) {
require_once RP_MODELS.'parcelle.class.php';
require_once RP_MODELS.'decoupage_territoire.class.php';
$parcelle = new parcelle($db->getInstance(), T_PARCELLE);
$decoupage_territoire = new decoupage_territoire($db->getInstance(), T_DECOUPAGE_TERRITOIRE);
$critereDecoup["decoup_terri_code"] = $_POST['dec_decoup_terri_code'];
$rue = $decoupage_territoire->lire($critereDecoup);
$critereDecoup["decoup_terri_code"] = $_POST['quartier_code'];
$quartier = $decoupage_territoire->lire($critereDecoup);
$critereDecoup["decoup_terri_code"] = $quartier["dec_decoup_terri_code"];
$commune = $decoupage_territoire->lire($critereDecoup);
$_POST['parcel_adresse'] = $_POST['parcel_porte_ppale'] . ", " . $rue["decoup_terri_nom"] . " - " . $quartier["decoup_terri_nom"] . " - " . $commune["decoup_terri_nom"];
$_POST['parcel_date_deb_construct'] = convertDateFormat5($_POST['parcel_date_deb_construct']);
$_POST['parcel_date_fin_construct'] = convertDateFormat5($_POST['parcel_date_fin_construct']);
if ($_POST['nouvel_enreg'] == 'yes') {
require_once RP_MODELS.'bien.class.php';
$bien = new bien($db->getInstance(), T_BIEN);
$_POST['bien_code_sig'] = $_POST['bien_ident'];
$bien->ajouter($_POST);
$bien_code = $bien->lireDernierId();
$_POST['bien_code'] = $bien_code;
$parcelle->ajouter($_POST);
$traceContent = "Ajout par mobile : " . $_POST["bien_code"] . " / " . $_POST["parcel_denomination"];
}
else {
$parcelle->modifier($_POST, array("bien_code"));
$traceContent = "Modification par mobile : " . $_POST["bien_code"] . " / " . $_POST["parcel_denomination"];
}
$critereTrace = array();
$critereTrace["trc_action"] = $traceContent;
$trace->ajouter($critereTrace);
}
问题是第一条记录只发布了!那么如何发布所有记录?
答案 0 :(得分:0)
而不是
if (c != null && c.moveToFirst()) {
do {
...
}while (c.moveToNext());
使用简单:
while(c.moveToNext()) {
...
}
更新
对于数据发送,我建议使用HttpClient
实施,例如AndroidHttpClient。它提供了用于构建请求的方便方法:
HttpClient httpClient = AndroidHttpClient.newInstance( "Android", this );
public List<List<NameValuePair>> getAllNewOrUpdatedParcelles() {
List<List<NameValuePair>> ret = new ArrayList<>();
String enreg, quartier_id = getCommune().getQuartierId();
String query = "SELECT * FROM " + T_PARCELLE + " WHERE bien_code != 0 AND ( updated = 'true' OR new_enreg = 'true')";
Cursor c = bd.rawQuery(query, null);
try{
while( c.moveToNext() ){
List<NameValuePair> curr = new ArrayList<>();
curr.add( new BasicNameValuePair( "data_parcelle", "yes" ) );
curr.add( new BasicNameValuePair( "bien_code", c.getString(0) ) );
// and so on
ret.add( curr );
}
}finally{ c.close(); }
return ret;
}
private void postData(String p_url) {
for( List<NameValuePair> params : getAllNewOrUpdatedParcelles() ){
try{
HttpPost post = new HttpPost( p_url );
httppost.setEntity( new UrlEncodedFormEntity( params, HTTP.UTF_8 ) );
HttpResponse hr = httpClient.execute( httppost );
Log.i( TAG, "result status = " + hr.getStatusLine().getStatusCode() );
}catch( IOException e ){
Log.i( TAG, "exception", e );
}
}
}