【发布时间】:2015-02-05 09:22:00
【问题描述】:
好的,伙计们,我有一个发票应用程序,它将发送存储在我的 Sqlite 中的发票列表,
我创建了一个转换器来从我的数据库中获取这些数据并通过 HttpPost 发送到我的服务器,但服务器只接受 ATOM/XML(而不是)或 XML ...我如何修改下面的此类以发送为XML 或 ATOM/XML ???有什么想法吗??
public class ItemNotaConverter {
public String toJSON(List<ItemNota> itemnotas) {
try {
JSONStringer jsonStringer = new JSONStringer();
jsonStringer.object().key("list").array().object().key("itemnota").array();
for (ItemNota itemnota : itemnotas) {
jsonStringer.object().
key("id_itemnota").value(itemnota.getId_itemnota()).
key("conjunto").value(itemnota.getConjunto()).
key("n_defeitos").value(itemnota.getNumeroDefeitos()).
key("problema").value(itemnota.getProblema()).
key("procedencia").value(itemnota.getProcedencia()).
key("descri_detalhes").value(itemnota.getDescricao_problema()).
endObject();
}
return jsonStringer.endArray().endObject().endArray().endObject().toString();
} catch (JSONException e) {
throw new RuntimeException(e);
}}}
我的 WebClient 类打开 HttpRequest
public class WebClient {
private final String url ;
public WebClient(String url) {
this.url = url;
}
public String post(String json) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(json));
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
String jsonDeResposta = EntityUtils.toString(response.getEntity());
return jsonDeResposta;
}catch (Exception e) {
throw new RuntimeException(e);
}}}
还有我的 AsyncTask 来执行“发送东西”
public class EnviaNotasTask extends AsyncTask<Object, Object, String> {
private final Context context;
private ProgressDialog progress;
private final String enderecourl = "MyUrl";
public EnviaNotasTask(Context context) {
this.context = context;
}
protected void onPreExecute() {
progress = ProgressDialog.show(context, "Aguarde...", "Envio de dados para Web", true, true);
}
protected String doInBackground(Object... params) {
Stara_DB dao = new Stara_DB(context);
List<Nota> lista = dao.getListaNota();
dao.close();
String listaJson = new NotaConverter().toJSON(lista);
String JsonResposta = new WebClient("MyUrl").post(listaJson);
return JsonResposta;
}
protected void onPostExecute(String result) {
progress.dismiss();
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}}
任何帮助都会得到真正的赞赏!!!
【问题讨论】:
标签: android xml json android-asynctask converter