i have this code running on android studio:
MainActivity.java
package myname.company.com.soap__03;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static HashMap<String, String> mHeaders = new HashMap<>();
static {
mHeaders.put("Accept-Encoding", "gzip,deflate");
mHeaders.put("Content-Type", "application/soap+xml");
mHeaders.put("Host", "server:port");
mHeaders.put("Connection", "Keep-Alive");
mHeaders.put("User-Agent", "AndroidApp");
// mHeaders.put("Authorization", "Basic Q2xpZW50NTkzMzppMjR3s2U="); // optional
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Button openButton = (Button) findViewById(R.id.open);
openButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
receiveCurrentShipments("WSDL URL");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}catch(Exception e) {
e.printStackTrace();
}
}
public final static InputStream receiveCurrentShipments(String stringUrlShipments) {
int status = 0;
String xmlstring = "<soapenv:Envelope xmlns:soapenv=\"http://someurl\" "+
"xmlns:soap=\"someurl\" "+
"xmlns:gen=\"http://someurl\"> "+
"<soapenv:Header> "+
"<auth:EventID xmlns:auth='http://someurl'>3000</auth:EventID> "+
"</soapenv:Header> "+
"<soapenv:Body> "+
"<gen:getGenericResult> "+
"<Request> "+
"<dataItem> "+
"<name>Username</name> "+
"<type>String</type> "+
"<value>usernamehere</value> "+
"</dataItem> "+
"<dataItem> "+
"<name>Password</name> "+
"<type>String</type> "+
"<value>passwordhere</value> "+
"</dataItem> "+
"</Request> "+
"</gen:getGenericResult> "+
"</soapenv:Body> "+
"</soapenv:Envelope>";
// StringBuffer chaine = new StringBuffer("");
HttpURLConnection connection = null;
try {
URL url = new URL(stringUrlShipments);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Length", xmlstring.getBytes().length + "");
connection.setRequestProperty("SOAPAction", "http://NAMESPACE\METHOD_NAME");
for (Map.Entry<String, String> entry : mHeaders.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
connection.setRequestProperty(key, value);
}
connection.setRequestMethod("POST");
connection.setDoInput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(xmlstring.getBytes("UTF-8"));
outputStream.close();
connection.connect();
status = connection.getResponseCode();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
Log.i("HTTP Client", "HTTP status code : " + status);
}
InputStream inputStream = null;
try {
inputStream = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimaryDark"
android:elevation="4dp"
android:paddingLeft="70dp"
android:paddingRight="70dp"
android:text="Soap Call"
android:textColor="#fff" />
</LinearLayout>
below is my output:
02/28 14:33:49: Launching app Cold swapped changes. $ adb shell am start -n "mokhethea.vodacom.com.soap__03/myname.company.com.soap__03.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Connected to process 30654 on device vodafone-vfd_500-TK8HFQ7PYHNNNBCE I/InstantRun: Instant Run Runtime started. Android package is myname.company.com.soap__03, real application class is null. E/MultiWindowProxy: getServiceInstance failed! D/libc-netbsd: [getaddrinfo]: hostname="WSDL URL and Port"; servname=(null); netid=0; mark=0 D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0 D/libc-netbsd: [getaddrinfo]: hostname="WSDL URL and Port"; servname=(null); netid=0; mark=0 D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0 I/HTTP Client: HTTP status code : 0
sample code was extracted from: Владимир Лапенков article: How to call a SOAP web service on Android
anyone can tell where i am getting wrong/ rather provide a working example..?