Reputation: 49
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 !!!
Upvotes: 0
Views: 5128
Reputation: 2097
Underscore-java library has static method U.jsonToXml(string)
. I am the maintainer of the project. Live example
import com.github.underscore.U;
public class MyClass {
public static void main(String args[]) {
String json = "{\"Price\": {"
+ " \"LineItems\": {"
+ " \"LineItem\": {"
+ " \"UnitOfMeasure\": \"EACH\", \"Quantity\": 2, \"ItemID\": \"ItemID\""
+ " }"
+ " },"
+ " \"Currency\": \"USD\","
+ " \"EnterpriseCode\": \"EnterpriseCode\""
+ "}}";
System.out.println(U.jsonToXml(json));
}
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<Price>
<LineItems>
<LineItem>
<UnitOfMeasure>EACH</UnitOfMeasure>
<Quantity number="true">2</Quantity>
<ItemID>ItemID</ItemID>
</LineItem>
</LineItems>
<Currency>USD</Currency>
<EnterpriseCode>EnterpriseCode</EnterpriseCode>
</Price>
Upvotes: 1
Reputation: 25008
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
Upvotes: 1