Post the data in Android using Http Client.

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”@drawable/travelling”
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” >

<EditText
android:id=”@+id/username”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textUsername”
android:layout_alignParentRight=”true”
android:layout_below=”@+id/textUsername”
android:layout_marginTop=”30dp”
android:ems=”10″ >

<requestFocus />
</EditText>

<TextView
android:id=”@+id/textUsername”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”24dp”
android:layout_marginTop=”50dp”
android:text=”Username”
android:textAppearance=”?android:attr/textAppearanceMedium”
/>

<TextView
android:id=”@+id/textpassword”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/username”
android:layout_below=”@+id/username”
android:layout_marginTop=”33dp”
android:text=”Password”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<EditText
android:id=”@+id/password”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textpassword”
android:layout_alignRight=”@+id/username”
android:layout_below=”@+id/textpassword”
android:layout_marginTop=”28dp”
android:ems=”10″
android:inputType=”textPassword” />

<TextView
android:id=”@+id/textmess”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textUsername”
android:layout_alignParentTop=”true”
android:layout_alignRight=”@+id/username”
android:layout_marginTop=”19dp”
android:text=”” />

<Button
android:id=”@+id/reg”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/login”
android:layout_alignBottom=”@+id/login”
android:layout_alignRight=”@+id/password”
android:layout_marginRight=”45dp”
android:text=”Register” />

<Button
android:id=”@+id/login”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/password”
android:layout_marginRight=”32dp”
android:layout_toLeftOf=”@+id/reg”
android:text=”Login” />

<TextView
android:id=”@+id/t”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textmess”
android:layout_alignParentTop=”true”
android:layout_alignRight=”@+id/textmess”
android:layout_marginTop=”14dp”
android:text=” ” />

</RelativeLayout>

 

 

//Now create java file for this layout.

(main.java)

package com.example.sofatsms2;

import java.io.BufferedReader;

import java.io.IOException;

import java.iO.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.*;

import android.os.*;

import android.widget.*;

import android.view.*;

import android.content.*;

import android.app.*;

class main extends Activity{

Context h;
TextView t;
EditText user;
EditText pass;
Button login;
Button regs;

protected static final String TAG = null;
@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

EditText jk=(EditText) findViewById(R.id.username);
t=(TextView) findViewById(R.id.t);
user=(EditText) findViewById(R.id.username);
pass=(EditText) findViewById(R.id.password);
regs=(Button) findViewById(R.id.reg);

regs.setOnClickListener(new OnClickListener(){

@Override

public void onClick(view v){

}

});

login=(Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
adshow();
}
});

public void adsshow(){

AlertDialog.Builder dialog=new AlertDialog.Builder(this);

dialog.setTitle(“ARE YOU READY”);

dialog.setMessage(“…….”);

String users=user.getText().toString();
String passs=pass.getText().toString();

DownloadWebPageTask task = new DownloadWebPageTask();
task.uname=users;
task.upass=passs;

//Toast.makeText(null, “Ok You Ready Now”, Toast.LENGTH_SHORT).show();

task.execute(new String[] { “http://202.164.53.122/~hitesh/login.php&#8221; });

AlertDialog alertDialog=dialog.create();
alertDialog.show();

}

class DownloadWebPageTask extends AsyncTask<String,void,String>{

String uname;
String upass;
String response = “”;

@Override

protected String doInBackground(String.. urls){

for(String url:urls){

DefaultHttpClient client=new DefaultHttpClient();

HttpPost httpget=new HttpPost(url);

HttpGet httppost=new HttpGet(url);

List<NameValuePair> namevaluepair=new ArrayList<NameValuePair>();

namevaluepair.add(new BasicNameValuePair(“name”,name));

namevaluepair.add(new BasicNameValuePair(“pass”,pass));

try{

httpget.setEntity(new UrlEncodedFrorEntity(namevaluepair));

}catch(UnsupportedEncodingException e)

{

e1.printStackTrace();

}try{

HttpResponse executee=client.execute(httpget);

InputStream content=executee.getEntity().getContent();

BufferedReader buffer=new BufferedReader(new InputStreamReader(content));

String s=””;

while((s=buffer.readLine())!=null){

response+=s

}

}catch(Exception e){

e.printStackTrace();

}

}

return response;

}

@Override

protected void onPostExecute(String result){

t.setText(result);
super.onPostExecute(result);
if(result.equals(“ok”)){
Intent ent=new Intent(MainActivity.this,welcome.class);
ent.putExtra(“name”, uname);
startActivity(ent);
}

}

}

}

 

Screenshot from 2014-01-17 00:37:26

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s