Anptk
Anptk

Reputation: 1123

How can i get data in Servlet, which is Sending from Android app?

I am new in android, i have searched for solution, but i could not get the suitable one, that s why i am posting this Qn,

I can't get any value in servlet,And no Errors in LogCat, Some Toast are in my code to check execution flow, The all Toast will work only in the first attempt, If i click my button second time, Only the first Toast will work,

Please help me to find a solution,

This is my android code

public void onClick(View v) {
    final   String u=txt_name.getText().toString();
    final   String p=txt_pswd.getText().toString();
    Toast.makeText(getApplicationContext(), u+p,Toast.LENGTH_LONG).show();
    new AsyncTask<String , Void, Void>() {

        @Override
        protected Void doInBackground(String... params) {

            try{
                //Log.d("Asynctask", ""+params);  
                //Looper.prepare();                                                 

                URL url=new URL("http://10.0.2.2:8080/LoginExample/LoginServlet");

                HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("GET");
                //Toast.makeText(getApplicationContext(), "connecting..",Toast.LENGTH_LONG).show();
                urlConnection.connect();
                //Toast.makeText(getApplicationContext(), "connected",Toast.LENGTH_LONG).show();
                urlConnection.getOutputStream().write(("key1="+u+"&key2="+p).getBytes());
                //Toast.makeText(getApplicationContext(), "sending....",Toast.LENGTH_LONG).show();
            }catch(Exception e)
            {
                System.out.println("ERROR IN URL CONNECTION---"+e);
            }                                       

            //Looper.loop();
            return null;
        }
    }.execute();    
});

And this is my servlet,

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
                   throws ServletException, java.io.IOException {

        try
        {       
            System.out.println("-----servlet--------------");
            // UserBean user = new UserBean();
            String uname=request.getParameter("key1");
            String password=request.getParameter("key2");

            System.out.println("uname ins ervlet==="+uname);

            System.out.println("password in servlet==="+password);
        }               
        catch (Throwable theException)      
        {
            System.out.println(theException); 
        }

    }

}

Upvotes: 0

Views: 826

Answers (2)

Anptk
Anptk

Reputation: 1123

This is my solution,

public void onClick(View v) {
    final   String u=txt_name.getText().toString();
    final   String p=txt_pswd.getText().toString();
        Toast.makeText(getApplicationContext(), u+p,Toast.LENGTH_LONG).show();
        new AsyncTask<String , Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {

                try{

                        URL url=new URL("http://10.0.2.2:8080/LoginExample/LoginServlet?"+"key1="+u+"&key2="+p);

                    HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();

                    urlConnection.setRequestMethod("GET");
                    urlConnection.connect();
                    BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    String result = br.readLine();
                    Log.d("MainActivity", result);

                }catch(Exception e)
                    {
                    e.printStackTrace();    
                    System.out.println("ERROR IN URL CONNECTION---"+e);
                    }



            //  Looper.loop();
                return null;
            }
        }.execute();
    }   

Upvotes: 0

Hardik Trivedi
Hardik Trivedi

Reputation: 6082

AsyncTask is not written correctly. There is NO NEED to write Looper in doINBAckground. In ideal cases,doInBackground don't deal with UI elements. Remove Toasts statement too. Use Log class to print log.

You request part seems wrong. If its get tye request try

URL url=new URL("http://10.0.2.2:8080/LoginExample/LoginServlet?"+"key1="+u+"&key2="+p);

Also check Internet permission in your manifest file.

Provide Stacktrace of your error.

Upvotes: 1

Related Questions