Android クライアントから HttpServlet にデータを送信しようとしています。私のサーブレットコードは次のとおりです
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection connection= null;
/**
* @see HttpServlet#HttpServlet()
*/
public DBServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(request.getParameterNames().hasMoreElements()){
insertRecord(request, getDbConnection());
}
else{ System.out.println("Servlet Started First Time"); }
}
/**
* Return database connection
* @return
*/
private Connection getDbConnection(){
String connectionURL = "jdbc:mysql://127.0.0.1:3306/testDB";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* Insert record in database
* @param request
* @param connection
*/
private void insertRecord(HttpServletRequest request, Connection connection){
System.out.println("===== Received Record =====");
PreparedStatement stmt;
try {
stmt = connection.prepareStatement("insert into users(Name,email,password) values(?,?,?)");
stmt.setString(1, request.getParameter("name"));
stmt.setString(2, request.getParameter("e-mail"));
stmt.setString(3, request.getParameter("password"));
int i= stmt.executeUpdate();
if(i != 0){
System.out.println("data inserted !!!");
}
else{
System.out.println("data failed to insert");
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
私のAndroidコードは
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.example.messenger.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SignUp extends Activity {
Button register;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
register = (Button)findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("OnClick", "=== onClick ===");
registerUser();
}
});
}
@SuppressLint({ "NewApi", "NewApi", "NewApi" })
private void registerUser(){
String name= ((EditText)findViewById(R.id.name)).getText().toString();
String email= ((EditText)findViewById(R.id.email)).getText().toString();
String password= ((EditText)findViewById(R.id.password)).getText().toString();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url=new URL("http://192.168.1.10:8080/DBservlet/DBServlet");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
DataOutputStream dos=new DataOutputStream(con.getOutputStream());
dos.writeBytes(name);
dos.flush();
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
問題は、「サーブレット」を実行すると「サーブレットが初めて開始されました」という出力が得られることですが、これは問題ありませんが、Android クライアントを実行して EditText フィールドにデータを書き込んで登録ボタンを押しても何も起こりません。「サーブレット」は何も受け取りません。この問題を解決する方法を教えてください。
よろしく、