How to create a web server using android studio? - android

I want to send some data collected by the app to the computer through internet. And secondly, apart from this, want to show live feed of the camera on the computer via internet.
I think making a web server will be a good option for the first requirement. But as I am new to android development, so please guide me.
For the live feed option, please suggest how this can be acheived.

Im not sure if this is good idea if you are new to android. Maybe start with something easier but if you do plan to do it any way.
To send data collected by the app you will have to send it to some kind of backend you will have to make it i suggest making it in PHP and saving the data in a Mysql database.
For the sending part you will probably want to do it in the background with a AsyncTask and it will look something like this :
public class Backend extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String server = "http://example.com/foo/bar"
String uid = params[0];
String name = params[1];
String email = params[2];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(server+"NewUser.php");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("uid", uid));
nameValuePair.add(new BasicNameValuePair("email", email));
nameValuePair.add(new BasicNameValuePair("name", name));
try {
// Probeer dat te zetten
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
// Execute
HttpResponse response = httpClient.execute(httpPost);
// Zet response om naar String
String responseBody = EntityUtils.toString(response.getEntity());
return responseBody;
} catch (ClientProtocolException e) {
e.printStackTrace();
return "0";
} catch (IOException e) {
e.printStackTrace();
return "0";
}
}
}
And you can execute this class like this:
new Backend().execute("1","maantje","email#example.com")
This will now POST uid, email, name to example.com/foo/bar/NewUser.php
So you will have to create that the data will be in $_POST['uid],$_POST['email'] and $_POST['name'].
Also don't forget the internet permissions in your manifest
<uses-permission android:name="android.permission.INTERNET" />
For the live feed option i have no experience with that but maybe this is a good place to start https://github.com/fyhertz/libstreaming

You are right. You need server which can handle your request from phone. Basically you need web-service which is called api. You can create those api by using java,node.js etc and you need database to store your data. For database purpose, you can use MongoDB or any other database.
Your api will handle POST request to hold the data from user request from the app. This is how, you can hold user data.
For live feed of the camera, I am not sure. Expecting to hear from some expert guys. Thanks and best of luck

Related

How can I pass data from my android application and receive it on my ASP.NET MVC website?

I want to send the some varibeles from my android application to my ASP.NET website, so I can use it there and I don't know how to do that.
If your ASP.NET application has some type of public API that would allow it to interact with external applications, you should be able to make a Web Request to it and post the appropriate values that you needed.
I'm not terribly familiar with Android syntax, but an example like this one on making HTTP GET/POST requests from Android should point you in the right direction :
// Build your client
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("your-asp-mvc-application/Home/AcceptData");
// Build a collection of data that you want to send
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("username", "test_user"));
nameValuePair.add(new BasicNameValuePair("password", "123456789"));
// Encoding POST data
try
{
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
catch (UnsupportedEncodingException e) {
// log exception
e.printStackTrace();
}
// Make the request
try
{
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
}
catch (ClientProtocolException e)
{
// Log exception
e.printStackTrace();
}
catch (IOException e)
{
// Log exception
e.printStackTrace();
}
Basically, once you make make requests, you should be able to target your application and create a Controller Action that can actually accept what you are sending it :
public ActionResult AcceptData(string username, string password)
{
// Do something here
}
First, what form of ASP.NET are you using - Forms or MVC? Also, what do you mean by "send?" Where exactly do you want the data to end up and what exactly do you want your ASP.NET application to do once it receives the data? If you simply mean that you're creating data in your phone and you want your ASP.NET web site to be able to access it too, you can just insert the data into a database that your ASP.NET web site also has access to (e.g. through a web service call or something like that).

Access mysql database online android

We are building a web app and a mobile app, we are building the web app using the mysql database. I don't want to use a separate sqlite database for the mobile app, i want to use the same mysql database. Could anyone provide insights of this feasibility and sample snippet if available on how to connect to mysql database online and extract data
You need to expose your DB via a service layer with proper security.
Say your web app is example.com and you need to use login. So you would need a service for validating login which will accept username and password in POST data.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/validateuser");
// Add your data
List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (2);
nameValuePairs.add(new BasicNameValuePair("user", "foo"));
nameValuePairs.add(new BasicNameValuePair("pass", "md5_string_of_input"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Something like to post and get response from your web app. Note that this is not a complete answer what you are asking for cannot be answered here

Android Java SQL Connect

So i'm a web developer by trade but am making an app at the moment.
I've got a program on my PC which sends data from my PC to a database on my webhost.
What I'm after now, is to create an app that is able to download that data with a series of queries.
Are there any good resources around that would help me to develop some Java code, to connect to a server through my tablet when I'm out and about and download the information from my webhost when on a different wifi network?
Thanks chaps, any help is appreciated.
The way I handle it is that I just create a web page based on what I want to do. For example if I want to verify a login on an app I create a page that takes in a username and password.
On the device I just post the username and password values by using a HTTP post transaction.
Example:
public boolean LoginUser()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("your URL comes here...");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
DataCrypt crypt = new DataCrypt();
try
{
this.Cryptemail = crypt.bytesToHex(crypt.encrypt(this.Email));;
this.Cryptpass = crypt.bytesToHex(crypt.encrypt(this.Password));
}
catch (Exception e)
{
DebugLog.log(e.getMessage());
}
nameValuePairs.add(new BasicNameValuePair("Password", this.Cryptpass));
nameValuePairs.add(new BasicNameValuePair("Mail", this.Cryptemail));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
BasicHttpResponse response = (BasicHttpResponse) httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String response = EntityUtils.toString(entity);
return true;
}
catch (Exception e)
{
DebugLog.log(e.getMessage());
return false;
}
return false;
}
EDIT: So you can just make a page that takes in some parameters that you want to base your query on and return the result in some way (for example by XML).
Hope this helps you out! :)

How can I capture a response sent from android to Django server using views.py?

I am new to the client-server side programming so my question might be basic.
Basically, I am trying to send data in JSON format from android to a Django server. The code for sending the data is the following:
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8000/androidweb/edit/");
JSONObject j = new JSONObject();
try {
j.put("name", "cdfe");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nameValuePairs.add(new BasicNameValuePair("year", j.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}catch(Exception e) {
//catch the exception and print it
}
So my intention is to basically call the url mentioned in code. I added the url to Django urls.py so I can use the views.py class to store the JSON data I entered above in a sqlite database table, which contains only one field called "name". However, I don't know if my approach is right. Most code samples I have seen pass the data to a php file, so I was wondering if it is possible to do it through a python class, views.py?
If it is possible, can you please give me a code sample to be implemented in "views.py" of how to capture JSON data sent from the above code and store it in a table with a "name" field?
Thanks!
Data sent via POST is available via request.POST. Try examining request.POST['year'].

how to send data from android to server

I'm exploring to scan ssid and rssi from android and I'm able to do it, but now i have want send the data to the server, so i explore i found below code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://101.34.45.45/rawData");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("userId", "00-22-68-E8-EC-F1"));
nameValuePairs.add(new BasicNameValuePair("timestamp", "2010-07-01 11:11:11"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
textView.setText(response.toString());
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
but now i'm having problem when add multi wifi data, the format should as below
http://101.34.45.45/rawData?data={"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]},
so how can done for the wifi parameter, can anyone help, I'm still trying for few varieties,
thanks
I would recommend you to switch to JSON for sending data to the server. Google gson is easiest to use to send and parse JSON.
{"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]}
You should use a JSON object having a JSON array as one of its items. The JSON array in turn contains another json object.
If you are using Google GSON, you should build a class hierarchy for the same. Heres how you should do the same.
Class data{
String userId;
String timestamp;
Wifi wifi;
}
Class Wifi{
String ssid;
int rssi;
}
You can check here for a sample code on a similar problem on parsing json in android.
This may be useful too.

Categories

Resources