Android错误org.xmlpull.v1.XmlPullParserException:意外

时间:2015-11-12 08:37:16

标签: android web-services

我有问题

org.xmlpull.v1.XmlPullParserException: Unexpected <! (position:START_DOCUMENT null@1:1 in java.io.InputStreamReader@21c87630)

当我尝试使用kso​​ap2在Android中调用Web服务时。

这是我的日志:

11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ org.xmlpull.v1.XmlPullParserException: Unexpected <! (position:START_DOCUMENT null@1:1 in java.io.InputStreamReader@21c87630)
11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.kxml2.io.KXmlParser.peekType(KXmlParser.java:1026)
11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.kxml2.io.KXmlParser.next(KXmlParser.java:346)
11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:2029)
11-12 15:10:59.982  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at com.example.vuong.final_project_pott.activity.SearchTicketActivity$JSonAsyncTask.doInBackground(SearchTicketActivity.java:156)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at com.example.vuong.final_project_pott.activity.SearchTicketActivity$JSonAsyncTask.doInBackground(SearchTicketActivity.java:108)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-12 15:10:59.992  14144-14316/com.example.vuong.final_project_pott W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

这是MainActivity:

    public class SearchTicketActivity extends ActionBarActivity implements View.OnClickListener {
    private EditText departure;
    private EditText arrival;
    private EditText date;
    private ListView listView;
    private TextView tvtest;
    private ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
    private ArrayList<TicketInforModel> ticketInforModelArrayList = new ArrayList<TicketInforModel>();
    private TicketInforModel ticketInforModel;
    private ArrayAdapter<TicketInforModel> arrAdapterTicket;
    private DatePickerDialog fromDatePickerDialog;
    private SimpleDateFormat dateFormatter;


   private final String TAG_IDTICKET = "ID_Ticket";
    private final String TAG_Company = "Company";
    private final String TAG_Departure = "Departure";
    private final String TAG_Arrival = "Arrival";
    private final String TAG_VEHICLESTYPE = "Vehicles_type";
    private final String TAG_SERVICES = "Services";
    private final String TAG_Date = "Date";
    private final String TAG_Time = "Time";
    private final String TAG_Price = "Price";
    private final String TAG_TicketStatus = "Ticket_Status";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchticket_main);

        departure = (EditText)findViewById(R.id.et_departure);
        arrival = (EditText)findViewById(R.id.et_arrival);
        date = (EditText)findViewById(R.id.et_date);
        listView = (ListView)findViewById(R.id.lv_ticket);
        tvtest = (TextView)findViewById(R.id.tv_test);

        dateFormatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

        setDateTimeField();
        Button btn_searching = (Button)findViewById(R.id.btn_searching);
        btn_searching.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSonAsyncTask jSonAsyncTask = new JSonAsyncTask();
                jSonAsyncTask.execute();
                if(arrayList.size() >0 ){
                    tvtest.setText("Done");
                }else{
                    tvtest.setText("Error");
                }


            }
        });
    }

    private void showMessages(String messages) {
        new AlertDialog.Builder(this).setTitle("Notification").setMessage(messages).setNeutralButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }

    @Override
    public void onClick(View v) {
        if(v == date) {
            fromDatePickerDialog.show();
        }
    }


    public void arrAdapter() throws JSONException {
        String TAG_IDTICKET = "ID_Ticket";
        String TAG_Company = "Company";
        String TAG_Departure = "Departure";
        String TAG_Arrival = "Arrival";
        String TAG_VEHICLESTYPE = "Vehicles_type";
        String TAG_SERVICES = "Services";
        String TAG_Date = "Date";
        String TAG_Time = "Time";
        String TAG_Price = "Price";
        String TAG_TicketStatus = "Ticket_Status";
        for(int i = 0; i < arrayList.size(); i++){
            ticketInforModel = new TicketInforModel();
            ticketInforModel.set_IdTicket(arrayList.get(i).getString(TAG_IDTICKET).toString());
            ticketInforModel.set_company(arrayList.get(i).getString(TAG_Company).toString());
            ticketInforModel.set_departure(arrayList.get(i).getString(TAG_Departure).toString());
            ticketInforModel.set_arrival(arrayList.get(i).getString(TAG_Arrival).toString());
            ticketInforModel.set_vehicletype(arrayList.get(i).getString(TAG_VEHICLESTYPE).toString());
            ticketInforModel.set_services(arrayList.get(i).getString(TAG_SERVICES).toString());
            ticketInforModel.set_date(arrayList.get(i).getString(TAG_Date).toString());
            ticketInforModel.set_time(arrayList.get(i).getString(TAG_Time).toString());
            ticketInforModel.set_price(Double.parseDouble(arrayList.get(i).getString(TAG_Price).toString()));
            ticketInforModel.set_ticket_Status(arrayList.get(i).getString(TAG_TicketStatus).toString());
            ticketInforModelArrayList.add(ticketInforModel);
        }
    }

    public void loadDataToListView() throws JSONException {
        listView = (ListView)findViewById(R.id.lv_ticket);
        arrAdapterTicket = new viewListTicket();
        listView.setAdapter(arrAdapterTicket);
    }

    class viewListTicket extends ArrayAdapter<TicketInforModel>{
        viewListTicket(){
            super(SearchTicketActivity.this,R.layout.ticket_design,ticketInforModelArrayList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.ticket_design, parent, false);
            }
            TicketInforModel currentTicket = ticketInforModelArrayList.get(position);
            TextView tv_time = (TextView)itemView.findViewById(R.id.tv_time);
            tv_time.setText(currentTicket.get_time());

            TextView tv_company = (TextView)itemView.findViewById(R.id.tv_company);
            tv_company.setText(currentTicket.get_company());

            TextView tv_phone = (TextView)itemView.findViewById(R.id.tv_phone);
            tv_phone.setText("1111");

            TextView tv_price= (TextView)itemView.findViewById(R.id.tv_price);
            tv_price.setText(((Double.toString(currentTicket.get_price())) ));

            TextView tv_service= (TextView)itemView.findViewById(R.id.tv_services);
            tv_service.setText(currentTicket.get_services());

            return itemView;
        }
    }
    private void setDateTimeField() {
        try{
            date.setOnClickListener(this);
            final Calendar newCalendar = Calendar.getInstance();
            fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();;
                    newDate.set(year, monthOfYear, dayOfMonth-1);
                    if(newDate.getTime().getTime() > (newCalendar.getTime().getTime())){
                        final AlertDialog builder = new AlertDialog.Builder(SearchTicketActivity.this).setTitle("Notification").setMessage("This is a date of future! We will get current date for this review!").show();

                        //this code below is coppied in https://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
                        final Timer t = new Timer();
                        t.schedule(new TimerTask() {
                            public void run() {
                                builder.dismiss(); // when the task active then close the dialog
                                t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                            }
                        }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
                        date.setText(dateFormatter.format(newCalendar.getTime()));

                    }

                    else{
                        newDate.set(year, monthOfYear, dayOfMonth);
                        date.setText(dateFormatter.format(newDate.getTime()));
                    }
                }
            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
        }catch(Exception ex){
            showMessages("Something Wrong!");
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




    public class JSonAsyncTask extends AsyncTask<Void, Void,Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(Void... params) {
            final String TAG_SOAPACTION1 = "http://tempuri.org/searchingTickets";
           final String TAG_NAMESPACE = "http://tempuri.org/";
            final String TAG_METHODNAME = "searchingTickets";
            final String TAG_URL = "http://www.vuongphgc60287.somee.com/mywebservices.asmx?op=searchingTickets";


            SoapObject request=new SoapObject(TAG_NAMESPACE, TAG_METHODNAME);


            PropertyInfo pi = new PropertyInfo();
            pi.name = "date";
            pi.setType(date.getText());
            pi.type = MarshalDate.DATE_CLASS;
            request.addProperty(pi);

            pi = new PropertyInfo();
            pi.name = "departure";
            pi.setType(departure.getText());
            pi.type = String.class;
            request.addProperty(pi);

            pi.name = "arrival";
            pi.setType(arrival.getText());
            pi.type = String.class;
            request.addProperty(pi);

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

            MarshalDate marshalDate = new MarshalDate();
            marshalDate.register(envelope);
            envelope.setOutputSoapObject(request);
            HttpTransportSE transport=new HttpTransportSE(TAG_URL,3000);
            transport.debug = true;
            try{
                transport.call(TAG_SOAPACTION1,envelope);
                SoapPrimitive data = (SoapPrimitive) envelope.getResponse();

                String jsonText=data.toString();
                JSONArray jsonArray=new JSONArray(jsonText);

                for(int i = 0; i<jsonArray.length();i++){
                    ticketInforModel = new TicketInforModel();
                    JSONObject jsonObject =(JSONObject)jsonArray.getJSONObject(i);
                    ticketInforModel.set_IdTicket(jsonObject.getString(TAG_IDTICKET).toString());
                    ticketInforModel.set_company(jsonObject.getString(TAG_Company).toString());
                    ticketInforModel.set_departure(jsonObject.getString(TAG_Departure).toString());
                    ticketInforModel.set_arrival(jsonObject.getString(TAG_Arrival).toString());
                    ticketInforModel.set_vehicletype(jsonObject.getString(TAG_VEHICLESTYPE).toString());
                    ticketInforModel.set_services(jsonObject.getString(TAG_SERVICES).toString());
                    ticketInforModel.set_date(jsonObject.getString(TAG_Date).toString());
                    ticketInforModel.set_time(jsonObject.getString(TAG_Time).toString());
                    ticketInforModel.set_price(Double.parseDouble(jsonObject.getString(TAG_Price).toString()));
                    ticketInforModel.set_ticket_Status(jsonObject.getString(TAG_TicketStatus).toString());
                    ticketInforModelArrayList.add(ticketInforModel);
                }

            }catch (JSONException e){
                Log.d("JSON",e.getMessage());
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
                Log.d("Loi gi day",e.getMessage());
            } catch (SoapFault soapFault) {
                soapFault.printStackTrace();
                Log.d("JSON",soapFault.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("JSON",e.getMessage());
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

        }
    }

这是我的JSON:

<string xmlns="http://tempuri.org/">
[{"ID_Ticket":"T01","Company":"Dien Linh","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"10:00","Price":1000000,"Ticket_Status":"Still"},{"ID_Ticket":"T02","Company":"Anh Tuyen","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"10:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T03","Company":"Son Lam","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"12:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T04","Company":"Phuong Trang","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"14:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T05","Company":"Mai Linh","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"16:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T06","Company":"To Chau","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"18:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T07","Company":"Le Chau","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"20:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T08","Company":"Phuong Nha","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"20:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T09","Company":"Kim Huong","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"22:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T12","Company":"Tuan Hung","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"21:00","Price":1200000,"Ticket_Status":"Still"},{"ID_Ticket":"T13","Company":"Hoang Anh","Departure":"Ho Chi Minh","Arrival":"Ha Noi","Vehicles_type":"vehicles sleepers","Services":"Wifi, cold tower, Water, Foods","Date":"08/13/2015","Time":"10:00","Price":1200000,"Ticket_Status":"Still"}]
</string>

这是.NET中的mywebservices

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace PTOT_Webservices_JSON
{
    /// <summary>
    /// Summary description for mywebservices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class mywebservices : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public String searchingTickets(String date, String departure, String arrival)
        {
            List<Ticket_Information> listTicket = new List<Ticket_Information>();
            String status;

                SqlConnection conn = new SqlConnection("server=.;database = OrderingTicketsTS; integrated security = true;");
                SqlCommand cmd = new SqlCommand("select * from ticket_information where Date = '" + date + "' and Departure = '" + departure + "' and arrival = '" +arrival+ "'", conn);
                conn.Open();
                //cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.AddWithValue("@date", date);
                //cmd.Parameters.AddWithValue("@Departure", departure);
                //cmd.Parameters.AddWithValue("@arrival", arrival);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Ticket_Information ticket_information = new Ticket_Information();
                        if (dr.GetBoolean(9) == true)
                        {
                            status = "Sold Out";
                        }
                        else
                        {
                            status = "Still";
                        }
                        ticket_information.ID_Ticket = dr.GetString(0);
                        ticket_information.Company = dr.GetString(1);
                        ticket_information.Departure = dr.GetString(2);
                        ticket_information.Arrival = dr.GetString(3);
                        ticket_information.Vehicles_type = dr.GetString(4);
                        ticket_information.Services = dr.GetString(5);
                        ticket_information.Date = dr.GetString(6);
                        ticket_information.Time = dr.GetString(7);
                        ticket_information.Price = dr.GetDouble(8);
                        ticket_information.Ticket_Status = status;
                        listTicket.Add(ticket_information);
                    }  
                }    
                return listTicket.ParserJSon();
                conn.Close();


        }
    }
}

这是.NET中传递者JSON的扩展类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace PTOT_Webservices_JSON
{
    public static class ExtensionClass
    {

            public static string ParserJSon(this object data)
            {
                JavaScriptSerializer sc = new JavaScriptSerializer();
                string strJson = sc.Serialize(data);
                return strJson;
            }
    }
}

您可以在here中查看我的网络服务。我在Google和Stackoverflow上找了一个小时来解决这个问题。我看到一些解决方案作为命名空间,我提供的URL不正确。我仔细检查并尝试更改URL,名称空间,但它不适合我。有人请给我这个问题的建议。我很生气。

0 个答案:

没有答案