I have some ksoap web service in timer action which is working every half an hour if I did it well. One of them send whole table rows to sql server and it turns me 0 or 1 depends on correctly sending. If it returns 1, it has to delete all row from sqlite.
Altough it returns 1, It does not get into if statements and does not work delete command.
I do not know my timer and thread is the right way for this and How can I do it correctly?
The Code:
try
{
final Timer V_Timer;
final Handler V_Handler;
V_Timer = new Timer();
V_Handler = new Handler(Looper.getMainLooper());
V_Timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
V_Handler.post(new Runnable()
{
public void run()
{
//for status update//
if(isConnected()){
Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_STATUS);
request.addProperty("MH_ID",hardwareID);
request.addProperty("Latitude",V_Latitude);
request.addProperty("Longitude",V_Longitude);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION_STATUS, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
final String str_ = response.toString();
//final String str = response.toString()+"\n"+ V_Latitude +"\n"+V_Longitude;
runOnUiThread (new Runnable(){
public void run() {
Toast.makeText(MainActivity.this, str_, Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
//Insert Bulk//
if(isConnected()){
Thread networkThread2 = new Thread() {
#Override
public void run() {
str = "0";
try {
StringBuilder bulk = GetTotalRecord();
bulkinsert = bulk.toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_BULK);
request.addProperty("Insert_Bulk",bulkinsert);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION_BULK, envelope);
final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
str = response.toString();
runOnUiThread (new Runnable(){
public void run() {
// Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
/////delete sqlite table/////
Log.d("str", str);
if (str=="1")
{
try {
dbobject.delete(SQLiteDB.TABLE_NAME_S, null, null);
Toast.makeText(MainActivity.this, "All Answers sent", Toast.LENGTH_LONG).show() ;
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "Error: Answers could not send \n "+e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
finally{
sqlitedb.close();
}
}
/////delete sqlite table/////
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread2.start();
//Insert Bulk end//
}
}
}});
}
},10000, 1000*60*30);
}
catch(Exception ex)
{
}
if (str=="1")
you can not compare String object this way in Java.
You should use equalsTo() or convert str to int
for instance:
if (str.equalsTo("1")) {
}
or
int strInt = Integer.parseInt( str )
if (strInt == 1) {
}
Related
initially, I am fetching image URL from server to a string then using Picasso library I am trying to load the image
I am able to get image URL from the server like thislogcat
but image not loaded in the image view. when tried placing direct URL it works.
public class MainActivity extends AppCompatActivity {
ImageView im;
Button bm;
String str ;
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_image);
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
bm = (Button) findViewById(R.id.btn_load_image);
bm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
im = (ImageView) findViewById(R.id.image);
Picasso.with(getApplicationContext()).load(str).into(im);
}
});
}
}
add this to your code
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
it will work
I have following code:
public class DoTask extends AsyncTask<Object, Object, Object> {
private HttpTransportSE mHttpTransportSE;
public DoTask() {
}
public void cancelTask(){
System.out.println("cancelling task 1");
if(mHttpTransportSE != null){
System.out.println("cancelling task 2");
mHttpTransportSE.reset();
try {
System.out.println("cancelling task 3");
mHttpTransportSE.getServiceConnection().disconnect();
cancel(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(Object itemImage) {
}
#Override
protected Object doInBackground(Object... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
mHttpTransportSE = getHttpTransportSE();
mHttpTransportSE.call(SOAP_ACTION, envelope);
mSuccess = true;
}
catch (Exception e){
System.out.println("other exception");
e.printStackTrace();
mSuccess = false;
mErrorMessage = "ERROR!";
}
return null;
}
private final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
return envelope;
}
private final HttpTransportSE getHttpTransportSE() {
HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY, MAIN_REQUEST_URL, 60000);
ht.debug = true;
ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");
return ht;
}
private String getValue(String value){
if(value == null || value.equals("anyType{}"))
return "";
return value;
}
}
I want to cancel the call when user clicks something. Tried to cancel it by using the method of cancelTask() called in my activity but I still can get the error message of catch (because the server is down right now, so it can't reach the server).
How can I cancel the ksoap call?
I'm trying to set a Toast when Ksoap get an exception on IOexception or XmlPullParserException. I'm trying with this code in EXCEPTION but i can't show toast.. i guess it's beacuse application got crashed and there is no time to show toast, but I'm not sure... need a help! thank you in advance:
public class RequestWS extends Activity {
private static final String NAMESPACE = "-------";
private static String URL="http://-------";
private static final String METHOD_NAME_SYNCHROAP = "-----";
private static final String SOAP_ACTION_SYNCHROAP = "-----";
private SoapObject request=null;
private SoapSerializationEnvelope envelope=null;
private Object resultsRequestSOAP=null;
public String requestSession() {
request = new SoapObject(NAMESPACE, METHOD_NAME_SESSION);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try {
transporte.call(SOAP_ACTION_SESSION, envelope);
resultsRequestSOAP = (Object)envelope.getResponse();
} catch (IOException e) {
RequestWS.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "ERROR", Toast.LENGTH_SHORT).show();
}
});
e.printStackTrace();
} catch (XmlPullParserException e) {
// NEED THE CORRECT CODE HERE
Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
String strJSON = resultsRequestSOAP.toString();
return strJSON;
}
}
I also tried this code inside EXCEPTION, but still not working
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "ERROR", Toast.LENGTH_SHORT).show();
}
});
runOnUiThread should have done the task.
One more thing you can try.
Declare a Handler inside your activity and pass it to this function.
public String requestSession(final Handler handler) {
...
} catch (IOException e) {
// NEED THE CORRECT CODE HERE'
handler.post(new Runnable(){
Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
});
e.printStackTrace();
} catch (XmlPullParserException e) {
// NEED THE CORRECT CODE HERE
handler.post(new Runnable(){
Toast.makeText(this, "ERROR", Toast.LENGTH_LONG).show();
});
e.printStackTrace();
}
...
}
i wrote a code using SOAP function to get from web service , the problem is that the response is not an xml string , what i need to know , the error from my android code SOAP function ? or from web service ?
why the result is liske this
public String SOAP_ACTION2 = "http://tempuri.org/GetDataTable";
public String OPERATION_NAME2 = "GetDataTable";
public String SOAP_ADDRESS2 = "http://192.168.0.15/EServicedesk/DesktopModules/EServiceDesk.Website/ESDWebService/Auth.asmx?WSDL";
Object response;
String XMLData = null;
SoapSerializationEnvelope envelope;
SoapRequestTask2 task;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task = new SoapRequestTask2();
try {
XMLData = task.execute().get();
Log.i("task ", XMLData);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class SoapRequestTask2 extends AsyncTask<Void, Void, String> {
// runs on ui thread.
private String data;
protected void onPreExecute() {
}
// runs in the background thread. do not update ui from here
protected String doInBackground(Void... params) {
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME2);
PropertyInfo pi = new PropertyInfo();
pi.setName("UserID");
pi.setValue(193);
pi.setType(Integer.class);
request.addProperty(pi);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS2);
response = null;
try {
// httpTransport.debug=true;
httpTransport.call(SOAP_ACTION2, envelope);
// String ss = httpTransport.requestDump;
response = envelope.getResponse();
data = response.toString();
// Log.i("respons", envelope.getResponse().toString());
} catch (Exception exception) {
response = exception.toString();
XMLData = response.toString();
}
return data;
}
here is the result :
task 2: anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; element=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{DocumentElement=anyType{TargetTable=anyType{RequestHotList=MyRequests; Count=927; }; TargetTable=anyType{RequestHotList=MyAssignedRequests; Count=603; }; TargetTable=anyType{RequestHotList=MyServiceDeskRequests; Count=969; }; TargetTable=anyType{RequestHotList=MyWorkGroupRequests; Count=770; }; TargetTable=anyType{RequestHotList=MyApproval; Count=82; }; }; }; }
you can do something like this
Web service returns array of object
you can get value for each value for each object by going to perticular node then use getProperty() method to get value for required object as follow.
//old web services.
public void getLocation()
{
OPERATION_NAME = "GetLd";
SOAP_ACTION = Constants.strNAMESPACE+""+OPERATION_NAME;
REQUST_ID=3;
final Responce responce=new Responce();
Thread thread=new Thread(new Runnable()
{
SoapObject response;
#Override
public void run()
{
try
{
SoapObject request=new SoapObject(Constants.strNAMESPACE, OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(Constants.strWEB_SERVICE_URL);
ht.call(SOAP_ACTION, envelope);
response = (SoapObject)envelope.bodyIn;
if(response!=null)
{
SoapObject str = null;
for(int i=0;i<response.getPropertyCount();i++)
str=(SoapObject) response.getProperty(i);
SoapObject str1 = (SoapObject) str.getProperty(0);
SoapObject str2 = null;
ArrayList<Location> arrData=new ArrayList<Location>();
for(int j=0;j<str1.getPropertyCount();j++)
{
str2 = (SoapObject) str1.getProperty(j);
Location location=new Location();
if(str2!=null)
{
responce.Result_Type=Constants.DataPresent;
if(isValidProperty(str2, "LocationName"))
{
location.setName(str2.getProperty("LocationName").toString());
}
if(isValidProperty(str2, "LocationId"))
{
location.setId(Integer.parseInt(str2.getProperty("LocationId").toString()));
}
arrData.add(location);
}
else
{
responce.Result_Type=Constants.NoDataPresent;
}
}
responce.ResponceData=arrData;
Log.d("Location", "------------------------");
for(int i=0;i<arrData.size();i++)
Log.d("Tag", arrData.get(i).toString());
}
}
catch (Exception e)
{
e.printStackTrace();
responce.Result_Type=Constants.Error;
}
finally
{
Message message=new Message();
message.what=REQUST_ID;
message.arg1=responce.Result_Type;
message.obj=responce;
handler.sendMessage(message);
}
}
});
thread.start();
}
boolean isValidProperty(SoapObject soapObject,String PropertyName)
{
if(soapObject!=null)
{
if(soapObject.hasProperty(PropertyName))
{
if(!soapObject.getProperty(PropertyName).toString().equalsIgnoreCase("")&&!soapObject.getProperty(PropertyName).toString().equalsIgnoreCase("anyType{}"))
return true;
else
return false;
}
return false;
}
else
return false;
}
i have to write above code in my button click event. webservice working fine.webservice method return a String. thatString is equal to success go to next layout.it's working fine. but else part not working. toast make a exception
final SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope1.setOutputSoapObject(request1);
//msg.setText("hi");
envelope1.dotNet = true;
final Thread webser=new Thread(){
public void run()
{
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
System.out.println("four and Object value is : " +androidHttpTransport);
System.out.println("four and URL : " +URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope1);
System.out.println("four a");
// Get the SoapResult from the envelope body.
SoapObject result1 = (SoapObject)envelope1.bodyIn;
if(result1 != null)
{
//Get the first property and change the label text
status=result1.getProperty(0).toString();
if(status.equalsIgnoreCase("success"))
{
Intent home=new Intent(LoginActivity.this,MainActivity.class);
startActivity(home);
}
else
{
Thread.sleep(1000);
Toast.makeText(LoginActivity.this,"Enter Valid Username/Password", Toast.LENGTH_LONG).show();
}
}
else
{
System.out.println("nodata");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception" +e);
}
}
};
webser.start();
}
});
You are accessing UI from back ground thread. To modife UI from Thread Use like this..
LoginActivity.this.runOnUiThread(new run Runnable() {
#Override
public void run() {
Toast.makeText(LoginActivity.this,"Enter Valid Username/Password", Toast.LENGTH_LONG).show();
}
});
And do not catch (Exception e). It is bad programming practice. :)