error occurs when requesting data from the internet in ksoap object
public String[] getProfile(String email) {
String[] profileArray = new String[3];
METHOD_NAME = "getProfile";
SoapObject getprofileSoapObject = new SoapObject(NAMESPACE,METHOD_NAME);
//To avoid code duplication getproperty. Set info
getprofileSoapObject.addProperty(setProperty("email", email));
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(getprofileSoapObject);
HttpTransportSE http = new HttpTransportSE(URL);
try { //call httptransport
http.call(SOAP_ACTION, envelope);
SoapObject SoapObject2 = (SoapObject) envelope.bodyIn;
//Set profile detail
profileArray[0] = SoapObject2.getProperty(0).toString();
profileArray[1] = SoapObject2.getProperty(1).toString();
profileArray[2] = SoapObject2.getProperty(2).toString();
return profileArray;
} catch (Exception e) {
//System.out.println(e.getMessage());
}
return profileArray;
}
NullPointerException error occurs after http.call
Related
I want to fetch a listview from webservice in Android app. I managed to get the data when i tried to break my code. But this is what I get when i break the code in the array list:
[com.xxx.example.Hotels#41319dc0, com.xxx.example.Hotels#4131a518, com.xxx.example.Hotels#4132fec8, com.xxx.example.Hotels#413323a0]
Below is my code that called the list.
mainListView = (ListView) findViewById(R.id.lview);
WebService2 cs = new WebService2();
listHt = cs.selectAllHotels();
listAdapter = new HotelArrayAdapter(this, listHt);
mainListView.setAdapter(listAdapter);
Below is the code called the webservice.
public ArrayList<Hotels> selectAllHotels() {
ArrayList<Hotels> lst = new ArrayList<Hotels>();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject table = null;
SoapObject responseBody = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
responseBody = (SoapObject) envelope.getResponse();
responseBody = (SoapObject) responseBody.getProperty(1);
table = (SoapObject) responseBody.getProperty(0);
for (int i = 0; i < table.getPropertyCount(); i++) {
Hotels dto = new Hotels();
SoapObject row = (SoapObject) table.getProperty(i);
dto.setName(row.getProperty("HOTELNAME").toString());
lst.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
}
return lst;
}
I am using Ksoap2 android assembly 2.4 in my android application.I am try to call the web service by using soap.But I am getting the error at transport.call(soapAction, envelop); this line.
LOGCaT:
org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}soap:Fault>#1:301 in java.io.InputStreamReader#41743310)
And Here my code is:
String url = "http://192.168.56.1:8080/CxfWebservice/webservices/Calculator";
// String namespace = "http://localhost:8080/wsdl";
String namespace = "http://192.160.59.1:8080/wsdl";
String methodname = "sum";
public static void SoapOperation(String url, String method_name,
String name_space) throws Exception {
String soapAction = name_space + method_name;
SoapObject request = new SoapObject(name_space, method_name);
PropertyInfo p = new PropertyInfo();
p.setName("arg0");
p.setValue(5);
p.setType(Integer.TYPE);
PropertyInfo p1 = new PropertyInfo();
p1.setName("arg1");
p1.setValue(15);
p1.setType(Integer.TYPE);
request.addProperty(p );
request.addProperty(p1);
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelop.setOutputSoapObject(request);
envelop.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
transport.call(soapAction, envelop);
String xml = transport.responseDump;
System.out.println("the response xml is:"+xml);
}
above is my code with please give me a solution for this.
Here is an example. Use this code
public void InteractWithWebService() {
try {
final String URL = con.getResources().getString(R.string.URL);
final String NameSpace = con.getResources().getString(
R.string.NAMESPACE);
final String MethodName = "sum";
final String SOAP_ACTION = con.getResources().getString(
R.string.SOAP_ACTION)
+ MethodName;
SoapObject Request = new SoapObject(NameSpace, MethodName);
Request.addProperty("param_name", Object_name.getText().toString()
.trim());
Request.addProperty("param_name", Object_name.getText().toString()
.trim());
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
htp.call(SOAP_ACTION, soapEnvelop);
// SoapObject response;
SoapPrimitive resultString = (SoapPrimitive) soapEnvelop
.getResponse();
if (resultString != null) {
status = Integer.parseInt(resultString.toString());
}
} catch (Exception ex) {
status = -1;
}
}
Add these lines in your strings file
<string name="NAMESPACE">http://tempuri.org/</string>
<string name="URL">http://Your_Localhost_address/Name_of_Service.svc</string>
<string name="SOAP_ACTION">http://tempuri.org/IName_of_Service/</string>
private final String zipCodenameSpace ="http://www.webserviceX.NET/";
private final String zipURL="http://www.webserviceX.net/uszip.asmx";
private final String zipSoapAction ="http://www.webserviceX.NET/GetInfoByCity";
private final String zipMethodName="GetInfoByCity";
SoapObject request = new SoapObject(zipCodenameSpace, zipMethodName);
PropertyInfo cityInfo = new PropertyInfo();
cityInfo.setName("USCity");
cityInfo.setValue(city);
// cityInfo.setType(String.class);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
request.addProperty(cityInfo);
TextView tv = (TextView) findViewById(R.id.tv1);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(zipURL);
try {
androidHttpTransport.call(zipSoapAction, envelope);
// Object response =(SoapObject) envelope.getResponse();
SoapObject response = (SoapObject) envelope.getResponse();
Log.d("This is an element", response.toString());
// tv.setText(response.toString());
// return response.toString();
} catch (Exception e) {
e.printStackTrace();
}
Showing null exception. Error is occurring at soapObject response.
I tried to use soap primitive but not working. Please help me where the problem is
Given Webservice returns xml.
cityInfo.setValue(city);
Where are you fetching this "city" value in your code? I think thats the problem. For testing you can set it as string directly here.like "NewYork".
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
I have a web service that I use in my android project. One method in my web service return null in android with ksoap2. When I debug the project, envelope is returning like this:
GPSYerBilgisiGetirResponse{GPSYerBilgisiGetirResult=anyType{}; }
Out of android, I run the web service and give parameter to method, returning data like this:
<ArrayOfFirma xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Firma>
<gpsilce>BEYOĞLU</gpsilce>
<gpssemt>KASIMPAŞA</gpssemt>
</Firma>
</ArrayOfFirma>
This is my function for using connect to web service
private String[] konumGetir(String ParamPK){
PropertyInfo pk = new PropertyInfo();
pk.name= "pk";
pk.setValue(ParamPK);
pk.type = PropertyInfo.STRING_CLASS;
SoapObject request = new SoapObject(NAMESPACE, "GPSYerBilgisiGetir");
request.addProperty(pk);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/GPSYerBilgisiGetir", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
konumList=new String[1];
for(int i=0;i<response.getPropertyCount();i++){
Object property = response.getProperty(i);
if(property instanceof SoapObject){
SoapObject firmalar = (SoapObject) property;
konumList[0]=firmalar.getProperty("gpssemt") + "-" + firmalar.getProperty("gpsilce");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return konumList;
}
This is my web service method:
<WebMethod()> _
Public Function GPSYerBilgisiGetir(ByVal pk As String) As List(Of Firma)
Dim konumlist As New List(Of Firma)
Dim dg As New dgetir("SELECT DISTINCT ILCE,SEMT FROM GPSYERBILGISI WHERE PK='" + pk + "'")
While dg.dtr.Read
Dim gpskonum As New Firma
gpskonum.gpsilce = dg.dtr.Item("ILCE")
gpskonum.gpssemt = dg.dtr.Item("SEMT")
konumlist.Add(gpskonum)
End While
dg.close()
Return konumlist
End Function
Try this code.
private String[] konumGetir(String ParamPK){
SoapObject request = new SoapObject(NAMESPACE, "GPSYerBilgisiGetir");
request.addProperty("pk", ParamPK);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION_NAME, envelope);
SoapObject result = (SoapObject) envelope.getResponse();
int childCount = result.getPropertyCount();
konumList = new String[childCount];
SoapObject tempArray[] = new SoapObject[childCount];
int i;
for (i = 0; i < childCount; i++) {
tempArray[i] = (SoapObject) result.getProperty(i);
konumList[i] = tempArray.getProperty(0).toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return konumList;
}
Hope it helps.