Код для просмотра в Android должен быть таким:
package com.campuspro.start;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login_Menu extends Activity {
EditText usname;
EditText pass;
TextView tv;
HttpClient client;
HttpPost post;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_lay);
tv=(TextView) findViewById(R.id.login_stat_tv);
usname=(EditText)findViewById(R.id.uname);
pass=(EditText)findViewById(R.id.pass);
Button login=(Button)findViewById(R.id.login_but);
Button cancel=(Button)findViewById(R.id.cancel_but);
client = new DefaultHttpClient();
String url="http://10.0.2.2:7001/proj/login.jsp";//your url
post = new HttpPost(url);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new Login().execute("");
}
});
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
usname.getText().clear();
pass.getText().clear();
}
});
}
private class Login extends AsyncTask<String, Void, JSONObject>{
ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating, Please wait...");
@Override
protected JSONObject doInBackground(String... params) {
Log.i("thread", "Doing Something...");
//authentication operation
try{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username",usname.getText().toString()));
pairs.add(new BasicNameValuePair("password",pass.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
int status=response.getStatusLine().getStatusCode();
if(status == 200)
{
HttpEntity e=response.getEntity();
String data=EntityUtils.toString(e);
JSONObject last=new JSONObject(data);
return last;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void onPreExecute(){
//dialog.dismiss();
Log.i("thread", "Started...");
dialog.show();
}
protected void onPostExecute(JSONObject result){
Log.i("thread", "Done...");
String status;
String name;
try {
status= result.getString("status");
name=result.getString("uname");
if(dialog!=null)
{
dialog.dismiss();
}
else{}
if(status.equalsIgnoreCase("yes"))
{
tv.setText("Login Successful...");
Bundle newbundle=new Bundle();
newbundle.putString("uname",name);
Intent myIntent=new Intent(Login_Menu.this,Instruction.class);
myIntent.putExtras(newbundle);
startActivity(myIntent);
}
else{
tv.setText("No User Found, please try again!");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
И код JSP на стороне сервера должен быть:
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.util.*,java.sql.*"%>
<%!
Connection con;
PreparedStatement ps;
ResultSet rs;
String x,y;
%>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","db_username","db_password");
x=request.getParameter("username");
y=request.getParameter("password");
ps=con.prepareStatement("select * from table_name where suid=? and spwd=?");
ps.setString(1,x);
ps.setString(2,y);
rs=ps.executeQuery();
JSONObject obj=new JSONObject();
if(rs.next())
{
String uname=rs.getString(4);
obj.put("status","yes");
obj.put("uname",uname);
out.print(obj);
}
else
{
obj.put("status","no");
out.print(obj);
}
out.flush();
%>