How to Connect SQL Server to Android Application

How to connect android application to MSSQL server and retrive data from it?

At last found a quiet good guideline to solve my problem. It tells me details about implementing web services and how to connect MSSQL server with Android.

This is more than helpful and details are found on the correct answer .

What do I need to do is, first of all, have to create an web service using .NET(C#) and then connect my android application to that web service to retrieve data from the local machine [for me its my MSSQL Server 2008 R2]. Hope this helps others.
But after implementing the ideas and codes provided in those links; I came up with some changes,particularly in the approach.

This is what I have done in my MainActivity.

Hope, this helps others.

 private static final String SOAP_ACTION = "http://tempuri.org/findContact";

private static final String OPERATION_NAME = "findContact";// your webservice web method name

private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx";

protected static final String TAG = null;
private static String fahren;

TextView tvData1;
EditText edata;
Button button;
String studentNo;
String state;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvData1 = (TextView)findViewById(R.id.textView1);
edata =(EditText)findViewById(R.id.editText1);

button=(Button)findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
studentNo=edata.getText().toString();
new Submit().execute(studentNo);
}
});
}

private class Submit extends AsyncTask<String, Void, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();

}

@Override
protected String doInBackground(String... arg) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.type = PropertyInfo.STRING_CLASS;
propertyInfo.name = "eid";
propertyInfo.setValue(studentNo);
request.addProperty(propertyInfo);//need to be careful adding this, as it became an issue for me while I was getting continuous exception.

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
try{
httpTransport.call(SOAP_ACTION, envelope);

Log.e("RequestDump", httpTransport.requestDump.toString());
Log.e("ResponseDump", httpTransport.responseDump.toString());

SoapObject result=(SoapObject)envelope.bodyIn;
if(result!= null){
state = result.getProperty(0).toString();
Log.e("Found", state);
}
else{
Log.e("Obj", result.toString());
}
}
catch (Exception exception) {
Log.e("Exception", exception.toString());
}
return state;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvData1.setText(result);
}
}
}

And of course, you cannot access webservice situated in your pc via wifi connection from your real device. [correct me if you know how to do it, please, provide correct url]. Here I ended up with emulator, and anyone can access live webservice from the real ip with their real device for sure, I have tested it already. This code works perfectly for me.
That's how I solved my problem.

I want to connect android application with sql server with web service

That's a pretty broad question, but here are the basics. A web service is a program or app that runs on a web server, usually exposed to the internet, or within a company's LAN on their intranet. Web Services can be built using ASP.NET or JAVA or PHP. I've used ASP.NET, specifically Web API 2.0, and Entity Framework. Entity Framework makes it very easy to connect your Web Service to your SQL Server database. With Web API and Entity Framework, you can create your web service and connect it to your database with basic Read/Add/Update/Delete capability in 1-2 hours.

Once you write your web service, you need to publish it on a Web Server as a Web App. I typically publish to an IIS Web Server (also Microsoft). For that you need a Website Hosted account with a Microsoft based server. Microsoft can host your Web Service using the Azure Cloud service. You can check out some tutorials for building your Web Service using the technologies mentioned and deploying to the Web using Azure. You can get a free Azure account to start with. Then, if you need to use it for a long time, you might need to pay for hosting fees.

This is probably the easiest and best way for you to get started, particularly if you are new to web services.

Good Luck!

Here is a tutorial using the technologies I've mentioned, except for Azure. But you should be able to find a tutorial for publishing a web service to Azure separately.

https://www.c-sharpcorner.com/article/asp-net-web-api-crud-logics-using-entity-framework-without-writing-single-code/



Related Topics



Leave a reply



Submit