这个错误来了
10月4日至18日:47:05.441 7383-7516 / in.spotrack.soubhagya E / AndroidRuntime:致命异常:的AsyncTask#2进程:in.spotrack.soubhagya,PID:7383了java.lang.RuntimeException:执行时发生错误doInBackground()在android.os.AsyncTask $ 3.done(AsyncTask.java:325)在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)在java.util.concurrent.FutureTask.setException(FutureTask.java :223)在java.util.concurrent.FutureTask.run(FutureTask.java:242)在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)在java.util.concurrent.ThreadPoolExecutor.runWorker(的ThreadPoolExecutor在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:607)在java.lang.Thread.run(Thread.java:761)所致1133):java.lang.ClassCastException:JAVA的.java . lang.Object []无法强制转换为java.lang.String [] at.spotrack.soubhagya.Employee_dashboard $ AsyncCallWS.doInBackground(Employee_dashboard.java:162)at android.os.AsyncTask $ 2.call(AsyncTask.java:305)在ja va.util.concurrent.FutureTask.run(FutureTask.java:237)at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133 )java.lang.Thread.run上的java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:607)(Thread.java:761)
Web服务类 .
public class WebService {
private static String NAMESPACE="http://tempuri.org/";
private static String URL ="http://www.soubhagyatech.com/webservice.asmx";
private static String SOAP_ACTION="http://tempuri.org/";
public static String invokeHelloWorldWS (String Email,String password, Integer usertype, String webMethName)
{
String loginStatus=null;
SoapObject request = new SoapObject(NAMESPACE, webMethName);
PropertyInfo EmailPI=new PropertyInfo();
PropertyInfo passwordPI=new PropertyInfo();
PropertyInfo usertypePI=new PropertyInfo();
//set first perameter in webservice
EmailPI.setName("Email");
EmailPI.setValue(Email);
EmailPI.setType(String.class);
request.addProperty(EmailPI);
Log.i("user",Email);
//set second perameter in websevice
passwordPI.setName("password");
passwordPI.setValue(password);
passwordPI.setType(String.class);
request.addProperty(passwordPI);
Log.i("pass",password);
//set third perameter in webservice
usertypePI.setName("usertype");
usertypePI.setValue(usertype);
usertypePI.setType(Integer.class);
request.addProperty(usertypePI);
Log.i("location", String.valueOf(usertype));
//create the object of SoapSerializationEnvelope
SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
Log.i("Envelope",soapSerializationEnvelope.toString());
//set the weservice application language
soapSerializationEnvelope.dotNet=true;
soapSerializationEnvelope.setOutputSoapObject(request);
//create the object of HttpTransportSE and pass the url of web service in this object as a perameter.
HttpTransportSE httpTransportSE=new HttpTransportSE(URL);
Log.i("httpTransportSE",httpTransportSE.toString());
try{
//invoice the web service
httpTransportSE.call(SOAP_ACTION+webMethName,soapSerializationEnvelope);
//get the response from the webservice
SoapPrimitive response= (SoapPrimitive) soapSerializationEnvelope.getResponse();
//get the status from the web service
loginStatus=response.toString();
Log.i("messge",loginStatus.toString());
}
catch (Exception e)
{
//set the errored variable as false
Admin_Login.errored=true;
e.printStackTrace();
}
//return loginstatus.
return loginStatus;
}
public static Boolean enterData(String webMethName)
{
SerilizationClassForAll serilizationClassForAll=new SerilizationClassForAll();
String Email=serilizationClassForAll.getEmail();
String userLoginAddress=serilizationClassForAll.getUserLoginAddress();
String userLogoutAddress=serilizationClassForAll.getUserLogoutAddress();
String userLoginLongitude=serilizationClassForAll.getUserLoginLongitude();
String userLoginLatitude=serilizationClassForAll.getUserLoginLatitude();
String userLogoutLatitude=serilizationClassForAll.getUserLogoutLatitude();
String userLogoutLongitude=serilizationClassForAll.getUserLogoutLongitude();
boolean status=false;
SoapObject request = new SoapObject(NAMESPACE, webMethName);
//create the object for the property of the webservice method.
PropertyInfo EmailPI = new PropertyInfo();
PropertyInfo userLoginAddressPI = new PropertyInfo();
PropertyInfo userLogoutAddressPI = new PropertyInfo();
PropertyInfo userLoginLongitudePI = new PropertyInfo();
PropertyInfo userLoginLatitudePI = new PropertyInfo();
PropertyInfo userLogoutLatitudePI = new PropertyInfo();
PropertyInfo userLogoutLongitudePI = new PropertyInfo();
//set first perameter in webservice
EmailPI.setName("Email");
EmailPI.setValue(Email);
EmailPI.setType(String.class);
request.addProperty(EmailPI);
//Log.i("user",user);
//set second perameter in websevice
userLoginAddressPI.setName("userLoginAddress");
userLoginAddressPI.setValue(userLoginAddress);
userLoginAddressPI.setType(String.class);
request.addProperty(userLoginAddressPI);
//Log.i("pass",pass);
//set third perameter in webservice
userLogoutAddressPI.setName("userLogoutAddress");
userLogoutAddressPI.setValue(userLogoutAddress);
userLogoutAddressPI.setType(String.class);
request.addProperty(userLogoutAddressPI);
//Log.i("idelhour",idelhour.toString());
userLoginLongitudePI.setName("userLoginLongitude");
userLoginLongitudePI.setValue(userLoginLongitude);
userLoginLongitudePI.setType(String.class);
request.addProperty(userLoginLongitudePI);
userLoginLatitudePI.setName("userLoginLatitude");
userLoginLatitudePI.setValue(userLoginLatitude);
userLoginLatitudePI.setType(String.class);
request.addProperty(userLoginLatitudePI);
userLogoutLatitudePI.setName("userLogoutLatitude");
userLogoutLatitudePI.setValue(userLogoutLatitude);
userLogoutLatitudePI.setType(String.class);
request.addProperty(userLogoutLatitudePI);
userLogoutLongitudePI.setName("userLogoutLongitude");
userLogoutLongitudePI.setValue(userLogoutLongitude);
userLogoutLongitudePI.setType(String.class);
request.addProperty(userLogoutLongitudePI);
//SoapEnvelope soapEnvelope=new SoapEnvelope(SoapEnvelope.VER11);
//create the object of SoapSerializationEnvelope
SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
Log.i("Envelope",soapSerializationEnvelope.toString());
//set the weservice application language
soapSerializationEnvelope.dotNet=true;
soapSerializationEnvelope.setOutputSoapObject(request);
//create the object of HttpTransportSE and pass the url of web service in this object as a perameter.
HttpTransportSE httpTransportSE=new HttpTransportSE(URL);
Log.i("httpTransportSE",httpTransportSE.toString());
try{
//invoice the web service
httpTransportSE.call(SOAP_ACTION+webMethName,soapSerializationEnvelope);
//get the response from the webservice
SoapPrimitive response= (SoapPrimitive) soapSerializationEnvelope.getResponse();
//get the status from the web service
status=Boolean.parseBoolean(response.toString());
// Log.i("messge",loginStatus.toString());
}
catch (Exception e)
{
//set the errored variable as false
Employee_dashboard.errored=true;
e.printStackTrace();
}
return status;
//return msg;
}
}
我们可以通过它调用webservice的主类 .
public class Employee_dashboard extends AppCompatActivity implements
LocationListener {
TextView textView;
Switch switchAB;
TextView locationText1;
LocationManager locationManager;
String Email;
Location location;
List<Address> addresses;
static Boolean errored = false;
boolean displayLocation;
AsyncTask asyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_dashboard);
textView=findViewById(R.id.textView3);
locationText1 = (TextView)findViewById(R.id.locationText);
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);
asyncTask=new AsyncCallWS();
switchAB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
getLocation();
DateFormat df = new SimpleDateFormat("dd/MM/yy");
DateFormat tf = new SimpleDateFormat("HH:MM");
Date dateobj = new Date();
textView.setText(df.format(dateobj)+" "+tf.format(dateobj));
} else {
Toast.makeText(getApplication(), "OFF", Toast.LENGTH_SHORT)
.show();
getLocation();
DateFormat df = new SimpleDateFormat("dd/MM/yy");
DateFormat tf = new SimpleDateFormat("HH:MM");
Date dateobj = new Date();
textView.setText(df.format(dateobj)+" "+tf.format(dateobj));
}
}
});
return true;
}
void getLocation() {
try {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, (LocationListener) this);
}
catch(SecurityException e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
this.location=location;
locationText1.setText("Latitude: " + location.getLatitude() + "\n Longitude: " + location.getLongitude());
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
locationText1.setText(locationText1.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+
addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2));
SerilizationClassForAll serilizationClassForAll=new SerilizationClassForAll();
serilizationClassForAll.setEmail(Email);
serilizationClassForAll.setUserLoginAddress(addresses.get(0).getAddressLine(0)+", "+
addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2));
serilizationClassForAll.setUserLogoutAddress(",ebfsfsjfuse");
serilizationClassForAll.setUserLoginLatitude(String.valueOf(location.getLatitude()));
serilizationClassForAll.setUserLoginLongitude(String.valueOf(location.getLongitude()));
serilizationClassForAll.setUserLogoutLatitude(String.valueOf(location.getLatitude()));
serilizationClassForAll.setUserLogoutLongitude(String.valueOf(location.getLongitude()));
asyncTask.execute();
}catch(Exception e)
{
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(Employee_dashboard.this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
SharedPreferenceClass sharedPreferenceClass=new SharedPreferenceClass(Employee_dashboard.this);
Email=sharedPreferenceClass.getEmail();
Log.i("Email",Email);
super.onResume();
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//disLoc.setText(displayLocation);
//progressBar.setVisibility(View.INVISIBLE);
if (!errored) {
if (displayLocation) {
Toast.makeText(Employee_dashboard.this, "Data is inserted successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Some error is generated", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(), "Error occured in invoking webservice", Toast.LENGTH_LONG).show();
}
errored = false;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(String... strings) {
displayLocation=WebService.enterData("attendenceInsert");
return null;
}
}
}