我是Android的新手,当我从我的Android(Java)应用程序向.NET Web服务(JSON)发送日期时,我遇到了问题。我在here中看到了一些解决方案。但它对我不起作用。 there is my webservices
我的json数据:
[{"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"}]
这是我的MarshaDate课程:
@Override
public Object readInstance(XmlPullParser xmlPullParser, String namespace, String name, PropertyInfo propertyInfo) throws IOException, XmlPullParserException {
return IsoDate.stringToDate(xmlPullParser.nextText(),IsoDate.DATE_TIME);
}
@Override
public void writeInstance(XmlSerializer xmlSerializer, Object o) throws IOException {
xmlSerializer.text(IsoDate.dateToString((Date) o, IsoDate.DATE_TIME));
}
@Override
public void register(SoapSerializationEnvelope soapSerializationEnvelope) {
soapSerializationEnvelope.addMapping(soapSerializationEnvelope.xsd, "DateTime", Date.class, this);
}
这是我的MainActivity
private EditText departure;
private EditText arrival;
private EditText date;
private ListView listView;
private TextView tvtest;
private ArrayList<JSONObject> arrayList;
private ArrayList<TicketInforModel> ticketInforModelArrayList;
private TicketInforModel ticketInforModel;
private ArrayAdapter<TicketInforModel> arrAdapterTicket;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
@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("MM/dd/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 task=new JSonAsyncTask();
task.execute();
}
});
}
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 class JSonAsyncTask extends AsyncTask<Void, Void, ArrayList<JSONObject>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<JSONObject> doInBackground(Void... params) {
ArrayList<JSONObject> listJSon=new ArrayList<>();
String TAG_SOAPACTION1 = "http://tempuri.org/searchingTickets";
String TAG_NAMESPACE = "http://tempuri.org/";
String TAG_METHODNAME = "searchingTickets";
String TAG_URL = "http://vuongphgc60287.somee.com/mywebservices.asmx";
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
MarshaDate marshaDate = new MarshaDate();
marshaDate.register(envelope);
SoapObject request=new SoapObject(TAG_NAMESPACE, TAG_METHODNAME);
request.addProperty("date", date.getText());
request.addProperty("departure",departure.getText());
request.addProperty("arrival",arrival.getText());
envelope.setOutputSoapObject(request);
HttpTransportSE transport=new HttpTransportSE(TAG_URL);
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++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
listJSon.add(jsonObject);
}
}catch (JSONException e){
Log.d("JSON",e.getMessage());
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (SoapFault soapFault) {
soapFault.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return listJSon;
}
@Override
protected void onPostExecute(ArrayList<JSONObject> result) {
super.onPostExecute(result);
arrayList.clear();
arrayList.addAll(result);
}
}
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("Can not do that").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);
}
当我尝试输入日期=&#34; 08/13/2015&#34;:
时出现问题java.lang.RuntimeException: Cannot serialize: 08/13/2015
请给我一些解决此问题的建议。谢谢你。