Let's say if I have a database which contain:
ID: 1 | Name: hello | Content1: sample1 | Content2: sample2
And I have developed a WSDL webservice which have the result like this:
<name> hello </name>
<content1> sample1 </content1>
<content2> sample2 </content2>
I have used KSOAP2 to read the data from the webservice.
String NAMESPACE = "blablabla";
String METHOD_NAME = "RequestDetails";
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "blablabla";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = false;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport = new HttpTransportSE(URL, 1000000);
Log.i(TAG, "transport started and completed! ");
transport.call(SOAP_ACTION, soapEnvelope);
SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
constructor.setName(resultString.get("name").toString());
constructor.setContent1(resultString.get("content1").toString());
constructor.setContent2(resultString.get("content2").toString());
} catch(Exception e) {
e.printStackTrace();
}
list.add(construct);
And now I put this list into a customAdapter. Then, the result will be like:
Name
Content1
Content2
Is there anywhere for me to make the result be like?
Name
Content1
Name
Content2
As it is a listview, so i wish to have
Name
Content1 is position 0.
Name
Content2 is position 1.
all you need to do is make listview_item_row as
name
content1
name
content2
and while adding data to listview just check name and content,
ex:
if(content2 == null)
do not add name for content 2
if(content2 != null)
then jsonparsing be like
constructor.setName(resultString.get("name").toString());
constructor.setContent1(resultString.get("content1").toString());
constructor.setName(resultString.get("name").toString());
constructor.setContent2(resultString.get("content2").toString());
try following apprach
create model class
public class SampleModel {
String title;
String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
public class SampleClass extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String NAMESPACE = "blablabla";
String METHOD_NAME = "RequestDetails";
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "blablabla";
List<SampleModel> arrayList = new ArrayList<>();
try {
SampleModel model;
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = false;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport = new HttpTransportSE(URL, 1000000);
transport.call(SOAP_ACTION, soapEnvelope);
SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
model = new SampleModel();
model.setTitle(resultString.get("name").toString());
model.setContent(resultString.get("content1").toString());
arrayList.add(model);
if (resultString.get("content2").toString() != null) {
model = new SampleModel();
model.setTitle(resultString.get("name").toString());
model.setContent(resultString.get("content2").toString());
arrayList.add(model);
}
} catch (Exception e) {
e.printStackTrace();
}
// here pass this arraylist to customListAdapeter
//list.add(construct);
}
}
Related
public class MainActivity extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/xxxx/GetLatLong";
private static final String METHOD_NAME = "GetLatLong";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://xxxx:xxxx/xxxx.svc/soap";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
}
catch (Exception exception)
{
textView.setText(exception.toString());
}
}
}
This is an example of my code.
This returns a string with 3 data description latitude and longitude .
I want to handle them separately how can I manage this ?
I want to put it then on a map with that configuration.
Write code something like this
JSONArray array=new JSONArray("your string response in json format");
for(int count=0;count<array.length();count++)
{
JSONObject obj=array.getJSONObject(count);
String description=obj.optString("Description");
String latitude=obj.optString("Latitude");
String longitude=obj.optString("Longitude");
// Logic to display latlong on map
}
I use ksoap2 to connect .NET by web service.
This is my Dataset
public DataSet getphimall()
{
DataSet ds1 = new DataSet();
try
{
SqlConnection cnn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=EMHAUI;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sp_GetAllSemester_ad", cnn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds1);
return ds1;
}
catch (Exception e)
{
return null;
}
}
And this is my webservice
[WebMethod]
public DataSet getSM()
{
Class1 phim1 = new Class1();
return phim1.getphimall();
}
And this is my javaconnector class
public class getSM {
String tenphim;
String daodien;
private static final String SOAP_ACTION = "http://tempuri.org/getSM";
private static final String METHOD_NAME = "getSM";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:50532/wsAndroid.asmx";
public getSM getallphim()
{
SoapObject table = null;
SoapObject client = null;
SoapObject tableRow = null;
SoapObject responseBody = null;
AndroidHttpTransport transport = null;
SoapSerializationEnvelope sse = null;
//cái này trong tut viết thế, mình lười đổi tên
sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
sse.addMapping(NAMESPACE, "getSM", this.getClass());
sse.dotNet = true;
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
getSM setphim = new getSM();
try
{
client = new SoapObject(NAMESPACE, METHOD_NAME);
sse.setOutputSoapObject(client);
sse.bodyOut = client;
androidHttpTransport.call(SOAP_ACTION, sse);
responseBody = (SoapObject) sse.getResponse();
responseBody = (SoapObject) responseBody.getProperty(1);
table = (SoapObject) responseBody.getProperty(0);
tableRow = (SoapObject) table.getProperty(0);
setphim.daodien = tableRow.getProperty("ID").toString();
setphim.tenphim = tableRow.getProperty("SemesterName").toString();
return setphim;
} catch (Exception e)
{
setphim.daodien = e.toString();
setphim.tenphim = e.toString();
return setphim;
}
}}
But When I run my emulator, I have an error
org.xmlpull.v1.xmlpullparserexception expected start_tag error
Please help me! Thanks
There might be a few reasons for the exception that you are getting.
Wrong Parameters for the SOAP call : try to confirm if the values of NAMESPACE, ACTION, METHOD and URL are correct or not, by looking at the WSDL file
Invalid response from server : try to log the response that the server sends to you and check if you are getting correct well-structured XML or not
androidHttpTransport.debug = true;
//execute request
androidHttpTransport.responseDump; //response string from server
dotNet attribute of the envelope : try using soapEnvelope.dotNet=true
this is my web service .i want to fetch only name . but when i am running my application i am getting an error.
java.lang.NoClassDefFoundError:org.ksoap2.serilization.soapobject at my package name
public static String SOAP_ACTION1 = "http://tempuri.org/GetContact";
// private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
public static String NAMESPACE = "http://tempuri.org/";
public static String METHOD_NAME1 = "GetContact";
//private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://115.119.182.114/Rotaryclub/RotaryService.asmx";
TextView txt;
//ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.displaycontacts);
txt=(TextView) findViewById(R.id.textView11);
//CALL the web service method with the two parameters vname and nname
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty( "Cid","1");
request.addProperty("Position", "doctor");
request.addProperty("Name", "abhishek");
request.addProperty("ImageUrl", "test.jpg");
request.addProperty("PhoneNo", "4324434323");
request.addProperty("MobileNo", "4324434323");
request.addProperty("FaxNo", "43-434-34");
request.addProperty("EmailId", "abh22ishek#gmail.com");
request.addProperty("Address", "kalkere");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// Make the soap call.
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
// SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
// String strRes = result.toString();
if(result != null){
txt.setText("SOAP response:\n\n" + result.getProperty(2).toString());
}
i have to use 2 soap call url is use in one class.
my code is:
public class Orderinfo extends Activity {
private static final String SOAP_ACTION = "http://xcart.com/data";
private static final String METHOD_NAME = "data";
private static final String NAMESPACE = "http://xcart.com";
private static final String URL = "http://192.168.1.168:8085/XcartLogin/services/RetailerWs?wsdl";
private static final String URL1 = "http://192.168.1.168:8085/XcartLogin/services/TodayC?wsdl";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = (TextView) findViewById(R.id.textView44);
// TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
} catch (Exception e) {
e.printStackTrace();
}
HttpTransportSE ht1 = new HttpTransportSE(URL1);
try {
ht.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
SoapPrimitive s = response;
String str = s.toString();
String resultArr[] = str.split("&");//Result string will split & store in an array
TextView tv = (TextView) findViewById(R.id.textView43);
// TextView tv = new TextView(this);
for(int i = 0; i<resultArr.length;i++){
tv.append(resultArr[i]+"\n\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here have to display first soap call output is 3.the second soap call output is 2.but both are displayed 2 only.why what exception is here.please help me.
you have used one evelope for request in both calls, so called service method and params are the same
I am trying to create a new property inside a SoapObject that will contain simple string properties with attributes to them like this:
<Power Unit="kw">1500</Power>
here is the code i am using for my samples below.
final SoapObject powerObject= new SoapObject(namespace, "Power");
powerObject.addAttribute("Unit", getPowerUnit());
PropertyInfo powerObjectProperty = new PropertyInfo();
powerObjectProperty .setName("");
powerObjectProperty .type = String.class;
powerObjectProperty .setValue(getPower());
powerObjectProperty .addProperty(powerObjectProperty);
root.addSoapObject(powerObject); // this is my root for the hierarchy
The best that i could reach is the following:
<Power Unit="kw"><>1500</></Power>
i even tried to add everything as a string but that encodes the <> tags.
<Power Unit"kw">1500</Power>
I am using:
ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar on android.
Ok i have solved this issue, here is how:
I have created a new soap object called TextSoapObject
public class TextSoapObject extends SoapObject {
public static final String TAG = TextSoapObject.class.getSimpleName();
public TextSoapObject(String namespace, String name) {
super(namespace, name);
}
public String text;
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Next i overrided SoapSerialization envelope like this:
public class ValueSerializationEnvelope extends SoapSerializationEnvelope {
public ValueSerializationEnvelope(int version) {
super(version);
}
public static final String TAG = ValueSerializationEnvelope.class.getSimpleName();
#Override
public void writeObjectBody(XmlSerializer writer, KvmSerializable obj) throws IOException {
if (obj instanceof TextSoapObject) {
writer.text(((TextSoapObject) obj).getText());
}
super.writeObjectBody(writer, obj);
}
}
And that's it.
To use this you would do the following:
final TextSoapObject costOfRepairs = new TextSoapObject(namespace, "CostOfRepairs");
costOfRepairs.addAttribute("Currency", getCurrency());
costOfRepairs.setText(getRepairCosts() + "");
root.addSoapObject(costOfRepairs);
EDIT:
This issue has been recognized for the ksoap2 library and addressed here:
http://code.google.com/p/ksoap2-android/issues/detail?id=112
Should be fixed in the next ksoap2 Release.
Try the following code
SoapObject soOriginDestInfo = new SoapObject(namespace_ns, "OriginDestinationInformation");
SoapPrimitive spDeptDtTm = new SoapPrimitive(namespace_ns, "DepartureDateTime", "asdadsad");
spDeptDtTm.addAttribute("Unit", "kw");
soOriginDestInfo.addProperty("DepartureDateTime", spDeptDtTm);
request.addSoapObject(soOriginDestInfo);
You can add property in your soap object and pass any value you want.
SoapObject request = new SoapObject(NAMESPACE, NAME);
request.addProperty("powerInKw", 1500);
like a key value pair.
I'm consuming some .NET web services and need to addAttribute to an addProperty while using ksoap for this xml:
<Element FirstAttribute="int" SecondAttribute="double" />
Here's what I did (including some extra help looping and passing in a double) to pass in this request:
String xml = null;
final String NAMESPACE = "http://yournamespace.org/";
final String SOAP_ACTION = "http://yournamespace.org/NameOfYourOperation";
final String METHOD_NAME = "NameOfYourOperation";
final String URL = "http://whereyourserviceis.com/Service.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
for (int i=0; i < anArray.size(); i++)
{
Some_obj obj = anArray.get(i);
SoapObject attributes = new SoapObject(NAMESPACE, "Element");
int a = Integer.parseInt(obj.someIntString);
attributes.addAttribute("FirstAttribute", a);
Double v = Double.parseDouble(obj.someDoubleString);
attributes.addAttribute("Value", v);
request.addProperty("SecondAttribute", attributes);
//request.addSoapObject(request);
Log.d(TAG,"=======obj.someIntString======: " + a + "=======obj.someDoubleString========: " + v);
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalFloat md = new MarshalFloat();
md.register(envelope);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
try
{
HttpTransportSE transport = new HttpTransportSE(URL, 60000);
transport.debug = true;
transport.call(SOAP_ACTION, envelope);
xml = transport.responseDump.toString();
Log.d(TAG, "getUpdates===============" + xml);
}
catch(SocketException ex)
{
Log.e("SocketException : " , "Error on getUpdates() " + ex.getMessage());
}
catch (Exception e)
{
Log.e("Exception : " , "Error on getUpdates() " + e.getMessage());
}
Take note of the MarshalFloat part...this is there if you're passing in a double value so the soap can serialize properly.