marinajogy
Newbie
Hi
I am trying to connect to a web service (coded in ASp.net 3.5 Express edition) in Andoid Code, but I am getting the following error
android.os.NetworkOnMainThreadexception
when this line is executed
androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
1. My Web service code is as follows- which working(I tested from C# Windows application)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService2
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World,hhh";
}
}
}
2. My Android Code is as follows
package com.example.wstest;
import android.os.*;
import android.app.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.ViewGroup.LayoutParams;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private ProgressDialog pd;
private Thread SearchTread;
private static final String NAMESPACE="http://tempuri.org/";
private static final String URL ="http://localhost:1852/Service1.asmx"; //Web Service URL
private static final String HelloWorld_SOAP_ACTION ="http://tempuri.org/HelloWorld"; //specifies the action
private static final String METHOD_NAME1 ="HelloWorld"; //specify the method name what u r calling
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnDlg = (Button) findViewById(R.id.btnDlg);
btnDlg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StartThread();
}
});
}
protected void StartThread()
{
//show process dialog
//pd = ProgressDialog.show(this, "Job Searching", "Contacting Web Service, Please Wait...");
SearchTread = new Thread() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
};
SearchTread.start();
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
//this is the function that do the search
DoSearch();
}
};
protected void DoSearch()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//request.addProperty("methodName","HelloWorld");
//create soap client
SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//call the web method from the URL
try
{
androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
//android.os.NetworkOnMainThreadexception occurs
//get the response as raw;
SoapObject objs = (SoapObject)envelope.getResponse();
Toast.makeText(MainActivity.this , "DoSearch..objs", Toast.LENGTH_LONG).show();
}
catch(Exception ex)
{
Toast.makeText(MainActivity.this , ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I am trying to connect to a web service (coded in ASp.net 3.5 Express edition) in Andoid Code, but I am getting the following error
android.os.NetworkOnMainThreadexception
when this line is executed
androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
1. My Web service code is as follows- which working(I tested from C# Windows application)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebService2
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World,hhh";
}
}
}
2. My Android Code is as follows
package com.example.wstest;
import android.os.*;
import android.app.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.ViewGroup.LayoutParams;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private ProgressDialog pd;
private Thread SearchTread;
private static final String NAMESPACE="http://tempuri.org/";
private static final String URL ="http://localhost:1852/Service1.asmx"; //Web Service URL
private static final String HelloWorld_SOAP_ACTION ="http://tempuri.org/HelloWorld"; //specifies the action
private static final String METHOD_NAME1 ="HelloWorld"; //specify the method name what u r calling
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnDlg = (Button) findViewById(R.id.btnDlg);
btnDlg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StartThread();
}
});
}
protected void StartThread()
{
//show process dialog
//pd = ProgressDialog.show(this, "Job Searching", "Contacting Web Service, Please Wait...");
SearchTread = new Thread() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
};
SearchTread.start();
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
//this is the function that do the search
DoSearch();
}
};
protected void DoSearch()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//request.addProperty("methodName","HelloWorld");
//create soap client
SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//call the web method from the URL
try
{
androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
//android.os.NetworkOnMainThreadexception occurs
//get the response as raw;
SoapObject objs = (SoapObject)envelope.getResponse();
Toast.makeText(MainActivity.this , "DoSearch..objs", Toast.LENGTH_LONG).show();
}
catch(Exception ex)
{
Toast.makeText(MainActivity.this , ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
