Saturday, June 20, 2015

Connecting Android App to live Server

Main Activity.java




package com.example.check;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t=(TextView)findViewById(R.id.textView1);
new class2(t).execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
class class2 extends AsyncTask<String, Void,String>
{

TextView tt;
class2(TextView t)
{
tt=t;
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub

String s="http://vadivelan.esy.es/new.php";
try {
URL u=new URL(s);
URLConnection c=u.openConnection();
c.setDoOutput(true);

OutputStreamWriter uu=new OutputStreamWriter(c.getOutputStream());

String data=URLEncoder.encode("hi","UTF-8")+"="+URLEncoder.encode("Data tobe Posted","UTF-8");

//data+="&"+URLEncoder.encode("hi","UTF-8")+"="+URLEncoder.encode("Data tobe Posted","UTF-8");

uu.write(data);

uu.flush();

BufferedReader r=new BufferedReader(new InputStreamReader(c.getInputStream()));

return r.readLine();


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ERROR";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tt.setText(result);
}

}









Activity Main.xml




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="203dp"
        android:text="TextView" />
 
</RelativeLayout>








Android Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.check"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.check.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>







The PHP File on Server



<?php
$c=$_POST['hi'];
echo $c;

?>




Connect Android App to Sql Server ::

https://amitku.wordpress.com/2011/08/03/how-to-connect-and-access-sql-database-server-from-android-app/