我试图用JSP MySQL的下面是这两个文件的Android的代码连接和jsp请有它的既不工作也不看给我会有什么问题的任何错误?
Android的代码:
package com.example.prj;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button submit;
TextView tv;
EditText et1,et2;
String user,pass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.textView1);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
//user=et1.getText().toString();
//pass=et2.getText().toString();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
ArrayList<BasicNameValuePair> namevaluepair=new ArrayList<BasicNameValuePair>();
namevaluepair.add(new BasicNameValuePair("user", user=et1.getText().toString()));
namevaluepair.add(new BasicNameValuePair("pass", pass=et2.getText().toString()));
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://10.0.2.2:8080/login.jsp");
post.setEntity(new UrlEncodedFormEntity(namevaluepair));
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
InputStream in=entity.getContent();
try{
BufferedReader bf=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=bf.readLine())!=null){
sb.append(line);
}
String result=sb.toString();
if(result.equalsIgnoreCase("vinod")){
Intent i=new Intent(MainActivity.this,loggedin.class);
i.putExtra("key",user);
startActivity(i);
}else{
tv.setText("invalid");
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
Log.e("log_tag", "Error in http"+e.toString());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
JSP代码:
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%!
String username;
String password;
Statement stmt=null;
ResultSet rs=null;
Connection con=null;
%>
<%
String driver="com.example.mysql.Driver";
Class.forName(driver).newInstance();
try{
String url="jdbc:mysql://localhost/log";
con = DriverManager.getConnection(url,"root","");
stmt=con.createStatement();
}catch(Exception e){
e.printStackTrace();
}
if(!con.isClosed()){
username=request.getParameter("user");
password=request.getParameter("pass");
rs=stmt.executeQuery("select * from login");
while(rs.next()){
if(rs.getString("user")==username && rs.getString("pass")==password){
out.println("vinod");
System.out.println("vinod kakad");
}else{
out.println("vinod");
System.out.println("vinod kakad else part");
}
}
else{
out.println("lost");
System.out.println("vinod kakad else lost part");
}
}
%>