我是android的新,我已经寻找解决办法,但我无法得到合适的一个,这也是为什么我 张贴这QN,我怎样才能从Servlet发送数据,从Android应用发送?
我不能得到的servlet的任何值,并且没有错误在logcat中,一些Toast
是在我的代码检查执行流程,全Toast
将只在第一次尝试,如果我点击我的按钮第二次,只有第一个Toast
将工作,
请帮我找到了解决方案,
这是我的android代码
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();
});
,这是我的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);
}
}
}
约尺蠖删除代码,写从服务器的响应,但现在我得到了java.io.FileNotFoundException:http://10.0.2.2:8080/LoginExample/LoginServlet – Anptk 2014-09-23 11:13:33
明白了.........谢谢兄弟 – Anptk 2014-09-23 12:18:40