我的webservice返回字符串值“1”或“-1”,数字但是字符串。
我在使用无法从字符串转换为org.ksoap2.serialization.SoapPrimitive的Asunc任务时出错
完整的AsuncTask类代码:
public class LoginAsync extends AsyncTask<Void, Void, String> {
Activity mActivity;
Context context;
ProgressDialog progressDialog;
public LoginAsync(Activity activity, ProgressDialog progressDialog,
Context context) {
super();
this.progressDialog = progressDialog;
this.mActivity = activity;
this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
SoapPrimitive resultRequestSOAP = null;
try {
final String NAMESPACE = "http://ws.sams.com";
final String URL = "http://sams-app.com:8080/webService/services/LoginActvityWs?WSDL"; // usint
// //
// //
// //
// localhost
final String METHOD_NAME = "login";
final String SOAP_ACTION = "http://ws.sams.com/login";
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String user = prefs.getString("login1", "0");
String pass = prefs.getString("password1", "0");
// Calling Login Method
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// First Reques for USER NAME .
PropertyInfo pi = new PropertyInfo();
pi.setName("username");
pi.setValue(user);
pi.setType(String.class);
request.addProperty(pi);
// Second Reques for USER NAME .
PropertyInfo pi2 = new PropertyInfo();
pi2.setName("password");
pi2.setValue(pass);
pi2.setType(String.class);
request.addProperty(pi2);
// Getting Request Result , Will get TID .
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
resultRequestSOAP = (SoapPrimitive) envelope.getResponse();
return String.valueOf(resultRequestSOAP);
}
catch (SocketTimeoutException e) {
return "error";
}
catch (ConnectTimeoutException e) {
return "error";
}
catch (XmlPullParserException e) {
return "error";
} catch (IOException e) {
return "error";
}
catch (NullPointerException e) {
return "error";
}
}
@Override
protected void onPostExecute(String result) {
// If any error oceeared duaring get TID
if (result.equalsIgnoreCase("error")) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
mActivity);
alertDialog.setTitle("يوجد مشكلة بالاتصال او السيرفر");
alertDialog.setMessage("هل تود المحاولة مجددا ؟ ");
// Retry Button Action
alertDialog.setPositiveButton("نعم",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LoginAsync asynTask = new LoginAsync(mActivity,
progressDialog, mActivity
.getApplicationContext());
asynTask.execute();
}
});
// No Button Action
alertDialog.setNegativeButton("لا",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
alertDialog.show();
}
// IF pass or user Filed .
else if (Integer.parseInt(result.toString()) == -1) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
mActivity);
alertDialog.setTitle("اسم المستخدم او كلمة المرور خاطئة");
alertDialog.setMessage("هل تود اعادة تسجيل الدخول ");
alertDialog.setPositiveButton("نعم",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
alertDialog.setNegativeButton("لا",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
Toast.makeText(mActivity.getApplicationContext(),
"thank you for using SAMS app",
Toast.LENGTH_LONG).show();
mActivity.finish();
}
});
alertDialog.show();
}
else if (Integer.parseInt(result.toString()) == 0) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
prefs.edit().putInt("TID", Integer.parseInt(result.toString()))
.commit();
Toast.makeText(mActivity.getApplicationContext(),
result.toString(), Toast.LENGTH_LONG).show();
GetClassesListAsync asynTask = new GetClassesListAsync(mActivity,
progressDialog, context);
asynTask.execute();
}
// For correct Login !
else {
progressDialog.dismiss();
if (Integer.parseInt(result.toString()) >= 1) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
prefs.edit().putInt("TID", Integer.parseInt(result.toString()))
.commit();
Intent intent1 = new Intent(context, DashBoard.class);
mActivity.startActivity(intent1);
}
}
}
}
完整日志错误:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.ksoap2.serialization.SoapPrimitive
at com.app.sams.LoginAsync.doInBackground(LoginAsync.java:82)
at com.app.sams.LoginAsync.doInBackground(LoginAsync.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
答案 0 :(得分:7)
这件事发生在我身上,我从SoapPrimitive
更改为Object
,然后投了它,然后就可以了。所以试试吧。这可能不相关,但如果您使用 dotnet 编写您的网络服务,请确保使用 {{在您的信封中指定1}};
答案 1 :(得分:4)
我有同样的问题并像Jacky Nguyen所说的那样解决它。这是一个例子,希望它有所帮助!
替换:
SoapPrimitive resultRequestSOAP = null;
通过
Object resultRequestSOAP = null;
并替换:
resultRequestSOAP = (SoapPrimitive) envelope.getResponse();
通过
resultRequestSOAP = (Object) envelope.getResponse();
答案 2 :(得分:0)
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
string result = response.toString();