好的,我有一个发票应用程序,它会发送存储在我的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();
}}
任何有用的帮助都会变得非常有效!
答案 0 :(得分:1)
以下是您的JSON如何成为XML:
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
答案 1 :(得分:1)
Underscore-java库具有静态方法U.jsonToXml(string)。我是该项目的维护者。 Live example
import com.github.underscore.lodash.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));
}
}
输出:
<?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>