1

サーブレットからAndroidアプリケーションに文字列値を送信するには? Android UI画面からサーブレットにデータを送信し、それらの値をデータベースに既に保存されているデータと照合できます。しかし、UI画面でToastまたはTextViewの「View」を取得できません。実際の Android デバイスを使用します。AVDなし。サーブレットのコードは次のとおりです。

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class myServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response)
            throws ServletException, IOException {
            String username = req.getParameter("suserName");
          String password = req.getParameter("spassword");
                       try{
      Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();        
      String url = "jdbc:jtds:sqlserver://**********:1422/xxxxx";
       String d_username = "sa";
      String d_password = "112233";
      Connection con = DriverManager.getConnection(url, d_username, d_password);  
      String sql = "SELECT UserName, Password FROM Info";
      PreparedStatement ps = con.prepareStatement(sql);
      ResultSet rs = ps.executeQuery();
      String name = null;
      String pass = null;
      while(rs.next()){
       name = rs.getString(1).toString();
          pass = rs.getString(2).toString();
      }
      if(username.equals(name) && password.equals(pass)){
          System.out.println("iT mATHCHES");

      }else{
          System.out.println("iT Does Not Match");
      }

    } catch (Exception sqlEx) {
            System.out.println(sqlEx);
        } 
       PrintWriter out = response.getWriter();
       out.println("From Servlet");
       out.flush();   
}

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
And Here is the Activity;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignInForm extends Activity {
    EditText UserName;
    EditText Password;
    Button signIn;
    TextView signInText;
    // ============================================
    String name;
    String pass;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin_form);
        UserName = (EditText) findViewById(R.id.IdUserNameSingInForm);
        Password = (EditText) findViewById(R.id.IdPasswordSignInForm);
        signInText = (TextView)findViewById(R.id.IdTextViewSignIn);
        StrictMode.enableDefaults();
        new signIn().execute();
    }

    public class signIn extends AsyncTask<Void, Void, Void> implements
            OnClickListener {
        @Override
        protected Void doInBackground(Void... HttpClient) {
            signIn = (Button) findViewById(R.id.IdButtonSignInForm);
            signIn.setOnClickListener(this);
            return null;
        }

        @Override
        public void onClick(View v) {
            String S_username = UserName.getText().toString();
            String S_password = Password.getText().toString();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://***********:8084/myapp/xxxxxx");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("userName", S_username));
            nameValuePairs.add(new BasicNameValuePair("password", S_password));
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
                try {
                    httpClient.execute(httpPost);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    @SuppressWarnings("resource")
        public void myConnection()throws IOException{
            FileReader fr = null;
            BufferedReader br = null;
            fr = new FileReader("http://******:8084/myapp/xxxxxx");
            br = new BufferedReader(fr);



    String line = br.readLine();
            //signInText.setText(line);
            Toast.makeText(SignInForm.this, line, Toast.LENGTH_SHORT).show();
}
}
}

このスレッドは説明します。しかし、私の理解では...良いチュートリアルへの提案は大歓迎です。Java を使用しないことは範囲外です。

Android からサーブレットに文字列を送信する

4

0 に答える 0