Get data from phpmyadmin using another machine - android

I am building an android app with the following code:
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/tut.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
System.out.println("Exception 1 caugt");
}
This apps works fine in my computer.
I want everyone download this app can use it and read the data from phpmyadmin.
can any one teach my how to do this?
(I want the data in phpmyadmin can be read from public user.)
And do I need to change the code?
HttpPost httpPost = new HttpPost("http://XXXXXXXXX/tut.php");

To make your app available to everyone you have to host your data online. There are many ways you can achieve this and it depends a lot on what type of application you intend to distribute.
If you just want to start trying out how things work you can buy a normal web hosting which supports mysql and php. This will cost you between 30 to 60 euros for a year. You setup your database, upload your php api and your good to go.
If you want a more professional approach you can choose to host in the cloud via cloud services the like of Amazon Web Services and Microsoft's Azure. This has a big learning curve but it gives you total control over the server and also huge experience.

Related

How to speed up the web service in Android?

Recently I started learning android. Now I want know information about web services in android.
which web services are fast in android?
we need to use any libraries for web service fastness?
My main aim is decrease the web service loading time.
Today I observed that web services are taking more time in below API levels and somewhat better in above API levels.
Please any one suggest me how to decrease web service loading.
If you think a library can be used then you may consider using Square Retrofit. With some benchmarks tested, it is quite snappy when considered with async task. Also you can take a look at volley which is also an option in this case but I would recommend Retrofit for its ease of use. you can consider the following article for some metrics.
Android Async HTTP Clients: Volley vs Retrofit.
JSON based web service is lightweight than XML or any other format so even in the low network connectivity you can expect considerable good performance.
Here is a sample code to connect with JSON based web service
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("data", dataValue);
// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
It's always better to make network calls inside AsyncTask so that you can show progress bar while you are dealing with web service

Measuring my app's network usage

Naturally I want to keep the network usage of my android app as low as possible, the question is how to measure it.
I managed to capture traffic with tcpdump and open it in wireshark, is that the way to go?
I have practically no idea on what all that stuff in wireshark means, obviously I have to read up on it, I just wanted to ask if there's a tutorial or tool ot whatever specifically for the aforementioned purpose?
Here you have very simple tutorial about measuring network usage.
You can also download this application and try to decompile it and watch the code.
try to use TrafficStats to statistics the system traffic .
Statistics the app traffic . if you use HttpClient ,try
HttpGet httpRequest = new HttpGet("http://xx.com/*");
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
int flowBytes = entity.getContentLength() ; //Traffic statistics
if you use URLConnection , try
URLConnection conn = imageUri.toURL().openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
int flowBytes = conn.getContentLength() //Traffic statistics

How to send data from android to SQL Server 2008?

I have created one web site and android applcation and I have used backed as SQL Server 2008.
I have used web services to display data from server to android application for that i have used JSON and ksoap library and it is working fine. I can display data from server to android. But now i want to store data from android application to SQL Server 2008. So anyone please tell me the step or how i can do that. Please help.
You can connect to SQL Server database through webservice.
Here is the sample code to call the webservice.
String url = "your_webservice_URL";
try
{
HttpPost loginHttpPost = new HttpPost(url);
HttpContext localContext = new BasicHttpContext();
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("parameter1", new StringBody(value1));
multipartContent.addPart("parameter2", new StringBody(value2));
loginHttpPost.setEntity(multipartContent);
HttpClient objHttpClient = new DefaultHttpClient();
HttpResponse response = objHttpClient.execute(loginHttpPost,localContext);
}
catch (IOException e) {
e.printStackTrace();}
Here is the example source code to Connect Android to SQL sever via WCF using JSON
Example :
1. Connecting Android to SQL Server
2. Get Data using XML
3. Get Data using JSON
- Inserting Data into database using WCF Service

Windows authentication prior to posting data, what am I missing?

I have an android app that is working fine when connected to a production web server. A development server was created for testing future releases. It's an IIS server that's locked down with username/password.
I am trying to use httpclient.getCredentialsProvider() to send a username and password so I can authenticate to the page before doing anything else but it doesn't seem to be working correctly so I assume I am missing some code or doing something wrong.
I tried messing with the credentials and sending the port and full url but that didn't work either so I just switched it to null and -1 which from what I gather means it should work on any site any port, but every way I tried still got the same result of not authenticating.
Here is what I have now.
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);
httpclient.getCredentialsProvider().setCredentials(new AuthScope(null,-1), new UsernamePasswordCredentials("someusername", "somepassword"));
HttpPost httpost = new HttpPost("someURL");
Other non-relevant code to set Name Value Pair for posting
Then
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httpost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httpost);
IIS supports HTTP authentication methods like Basic, Digest and Integrated. The problem is that all of them are hardwired to Windows accounts. This means that you need a Windows user on your server for every account you want to HTTP-auth enable.
Having the ability to do plain Basic Authentication agains account stored e.g. in a database would be very handy for a range of situations like web applications, (WCF) web services, REST services, Silverlight service backends etc.
enter link description here

Android app accesses web service

I need to create an Android application, I'm not sure which is a better way of doing this by better I mean should I use the WebView or create an application .
I need to implement the existing application which is a ASP.NET application which mainly consists of a login screen, once the user logs in he will see a list a items in probably a gridview based on the selection from the gridview. He then will be shown more detailed info about the selected item.
The above is a web application I need to implement this as a app on Android phone.
Also there will be a need to use the GPS where based on the GPS values the department will be selected and also use the camera to take a picture and save it on to the server .
A solution which I was thinking of was to expose .NET web services and then access it in the android phone!
But I am very new to Android development and really do not how to go about this. Is there any better solution?
Can anyone help me as to how do I go about this ?
Pros:
Android App may work faster then web applications (but still depends on web page complexity)
By the help of this community and android developer site you can complete your app within a 2-3 weeks.
As you stated picture capture/upload and GPS etc are advantages of the smart phone app.
Cons:
Later, you may need iPhone, Blackberry apps!
Instead of .Net web service which typically returns XML, you can go for HTTP call with JSON response (I've seen it in Asp.net MVC). So that you can easily parse the data on android app.
Added:
HTTP call:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(getString(R.string.WebServiceURL) + "/cfc/iphonewebservice.cfc?returnformat=json&method=validateUserLogin&username=" + URLEncoder.encode(sUserName) + "&password=" + URLEncoder.encode(sPassword,"UTF-8"));
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
JSONObject JResponse = new JSONObject(sResponse);
String sMessage = JResponse.getString("MESSAGE");
int success = JResponse.getInt("SUCCESS")
There are two approaches available to you:
Build an Android app.
Build a webapp, using W3C geolocation to access GPS coordinates. (see geo-location-javascript)
If you go for option (1), you'll want to expose your .NET service as a simple REST API (using JSON as Vikas suggested to make it just that bit simpler!)
Android already comes with all the components needed to access and parse such a REST API, specifically the Apache HTTP and JSON packages, and can be iterated on rather quickly once you have the basic request/parse framework in place.

Categories

Resources