Java 我如何在Servlet中获取数据,它是从Android应用程序发送的?

Java 我如何在Servlet中获取数据,它是从Android应用程序发送的?,java,android,http,servlets,Java,Android,Http,Servlets,我是android新手,我一直在寻找解决方案,但我没有找到合适的解决方案,这就是我的原因 发布此Qn 我无法在servlet中获取任何值,LogCat中没有错误,我的代码中有一些Toast用于检查执行流,所有Toast仅在第一次尝试中有效,如果我第二次单击我的按钮,则只有第一个Toast有效 请帮我找到解决办法 public void onClick(View v) { final String u=txt_name.getText().toString(); final

我是android新手,我一直在寻找解决方案,但我没有找到合适的解决方案,这就是我的原因 发布此Qn

我无法在servlet中获取任何值,LogCat中没有错误,我的代码中有一些
Toast
用于检查执行流,所有
Toast
仅在第一次尝试中有效,如果我第二次单击我的按钮,则只有第一个
Toast
有效

请帮我找到解决办法

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();
    }   
这是我的安卓代码

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();    
});

AsyncTask未正确写入。不需要在doINBAckground中写入循环器。在理想情况下,doInBackground不处理UI元素。删除祝酒词也。使用日志类打印日志

你要求的部分似乎是错误的。如果收到请求,请尝试

URL url=new URL("http://10.0.2.2:8080/LoginExample/LoginServlet?"+"key1="+u+"&key2="+p);
还要检查清单文件中的Internet权限

提供您的错误跟踪。

这是我的解决方案

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();
    }   
public void onClick(视图v){
最后一个字符串u=txt_name.getText().toString();
最后一个字符串p=txt_pswd.getText().toString();
Toast.makeText(getApplicationContext(),u+p,Toast.LENGTH_LONG).show();
新建异步任务(){
@凌驾
受保护的Void doInBackground(字符串…参数){
试一试{
URL=新URL(“http://10.0.2.2:8080/LoginExample/LoginServlet?“+”键1=“+u+”&键2=“+p”);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.connect();
BufferedReader br=新的BufferedReader(新的InputStreamReader(urlConnection.getInputStream());
字符串结果=br.readLine();
日志d(“主要活动”,结果);
}捕获(例外e)
{
e、 printStackTrace();
System.out.println(“URL连接错误----”+e);
}
//loop.loop();
返回null;
}
}.execute();
}   

删除了关于looper的代码,并从服务器写入响应,但现在我得到了java.io.FileNotFoundException: