title: Sending String From Servlet To Android App
body: <p>How to send String value from Servlet to Android Application? I can send data from android UI screen to Servlet and match those values with the data already stored on the database. but i can not get Toast or TextView "View" on my UI screen. using real android device; no AVD. Here is the code for servlet:</p>
  
  <pre><code>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) &amp;&amp; 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";
      }// &lt;/editor-fold&gt;
  }
  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&lt;Void, Void, Void&gt; 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&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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();
  }
  }
  }
  </code></pre>
  
  <p>This thread describes; but out of my understanding...Any Suggestions to good tutorial is appreciated. Not using java is out of scope.</p>
  
  <p><a href="https://stackoverflow.com/questions/14258951/send-a-string-to-a-servlet-from-android">Send a string to a servlet from android</a></p>