我是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