如何将字符串中的XML格式作为PropertyInfo发送?

时间:2014-08-18 14:21:37

标签: android xml android-asynctask android-ksoap2

我正在使用KSOAP2库,在我的应用程序中,我必须以字符串形式发送一个xml文件作为PropertyInfo。 问题是在创建xml文件并将其作为String发送后,应用程序崩溃。

这是我的代码:

public class AsyncCommande extends AsyncTask<String, String,String>{
                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                }
                protected String doInBackground(String... params) {

                     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_SEND);

                     PropertyInfo pi =new PropertyInfo();
                     pi.setName("xDoc");
                     pi.setValue(convertPanierToString(panier_client));

                     pi.setType(String.class);
                     request.addProperty(pi);

                     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                     envelope.dotNet = true;
                     envelope.setOutputSoapObject(request);

                     HttpTransportSE androidHttpTransport = new HttpTransportSE(getURL());
                     androidHttpTransport.debug = true ;
                     try{
                         Log.i("-------",androidHttpTransport.requestDump);
                         androidHttpTransport.call(SOAP_ACTION_SEND, envelope);
                         final SoapObject response = (SoapObject)envelope.getResponse();
                         runOnUiThread(new Runnable() {
                         @Override
                         public void run() {
                          Log.i("Reponse",response.getProperty(0).toString());

                     }
                   });
                 }
                    catch(Exception e){
                     e.printStackTrace();

                    }
                    return null;
                }   
        }

这是xml文件,但它被视为String:

<?xml version="1.0" encoding="UTF-8"?>
       <Produits>
             <Produit>
                      <Nom>BOISSON ENERGISANTE</Nom>
                      <Quantity>2</Quantity>
             </Produit>
       </Produits>

这是logcut的输出:

08-18 14:16:41.354: W/System.err(1892): java.lang.NullPointerException: println needs a message
08-18 14:16:41.362: W/System.err(1892):     at android.util.Log.println_native(Native Method)
08-18 14:16:41.370: W/System.err(1892):     at android.util.Log.i(Log.java:160)
08-18 14:16:41.378: W/System.err(1892):     at guest.hotix.myhotix.CommanderService$AsyncCommande.doInBackground(CommanderService.java:300)
08-18 14:16:41.382: W/System.err(1892):     at guest.hotix.myhotix.CommanderService$AsyncCommande.doInBackground(CommanderService.java:1)
08-18 14:16:41.394: W/System.err(1892):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-18 14:16:41.394: W/System.err(1892):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 14:16:41.398: W/System.err(1892):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-18 14:16:41.402: W/System.err(1892):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-18 14:16:41.418: W/System.err(1892):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-18 14:16:41.422: W/System.err(1892):     at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

使用

等共享偏好设置
SharedPreferences sp;
SharedPreferences.Editor speditor;
Button log,reg;
EditText name,pwd;    
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    sp=getSharedPreferences("userdata",MODE_PRIVATE);
    speditor=sp.edit();
    name = (EditText) findViewById(R.id.editText1);
    pwd = (EditText) findViewById(R.id.editText2);
    log = (Button) findViewById(R.id.button1);
    log.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
          String n,p;
            n=sp.getString("NAME","");
            p=sp.getString("PASWRD","");

            Toast.makeText(getBaseContext(), "Welcome          "+name.getText().toString(),Toast.LENGTH_LONG).show();}});
     reg = (Button) findViewById(R.id.button2);
    reg.setOnClickListener(new View.OnClickListener() {
                        speditor.putString("NAME",name.getText().toString());
                speditor.putString("PASWRD",pwd.getText().toString());
                speditor.commit();
                Toast.makeText(getBaseContext(),"Hello "+name.getText().toString()+" You are Registered",Toast.LENGTH_LONG).show();
            }
    });

}