Issue
Okay guys, i got an Invoice app that will send a list of Invoices stored in my Sqlite,
and i create a converter to took thes data from my db and send via HttpPost to my server, but the server only accept, ATOM/XML(rather) or XML ... how can i modify this class below to send as XML or ATOM/XML ??? any ideas ??
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);
}}}
My WebClient Class to open the 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);
}}}
And my AsyncTask to perform the "sending stuff"
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();
}}
Any helps wil be trully aprecciated !!!
Solution
json.org, the mothership of JSON, provides a library that you can use.
Here is how your JSON becomes XML:
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
Source: Converting JSON to XML in Java
Answered By - An SO User
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.