Android链接Servlet异常,java - Problem connecting to servlet From Android client - Stack Overflow

I am trying to connect to the Servlet (Tomcat localhost). Here is my code.

ServletTest.java (the client)

public class ServletTest extends Activity {

private static final String TAG = "Test";

/** Called when the activity is first created. */

protected static EditText username;

protected static EditText password;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

username = (EditText)findViewById(R.id.username);

Button goButton = (Button)findViewById(R.id.connect);

goButton.setOnClickListener(mGoListener);

}

private OnClickListener mGoListener = new OnClickListener()

{

@Override

public void onClick(View v)

{

//finish();

String uname=username.getText().toString();

Log.v(TAG, uname);

networkthread ob = new networkthread(uname);

}

};

}

class networkthread implements Runnable {

private static final String TAG = "Test";

String uname;

public networkthread(String uname) {

this.uname = uname;

Thread t = new Thread(this);

t.start();

}

public void run(){

try{

Log.v(TAG,"Inside try");

Log.v(TAG,"before con");

URL url=new URL("http://192.168.1.117/servletAndroid/TestAndroid");

HttpURLConnection con;

con=(HttpURLConnection)url.openConnection();

Log.v(TAG,"after con");

con.setDoInput(true);

con.setDoOutput(true);

con.setRequestMethod("POST");

con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

con.setRequestProperty("Accept", "application/octet-stream");

Log.v(TAG,"Before dos");

DataOutputStream dos=new DataOutputStream(con.getOutputStream());

Log.v(TAG,"After dos");

dos.writeUTF(uname.trim());

dos.flush();

dos.close();

Log.v(TAG,"Finish try");

}

catch(Exception e){

Log.v(TAG,"Exception"+e);

}

}

}

TestServlet.java (the server)

public class TestServlet extends HttpServlet {

public void init() {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

try{

System.out.println("test");

DataInputStream in = new DataInputStream((InputStream) request.getInputStream());

String uname = in.readUTF();

//String password = in.readUTF();

System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");

System.out.println(":::::::::::" + uname);

response.setContentType("text/plain");

//response.setContentLength(info.length());

PrintWriter out = response.getWriter();

out.println(uname);

in.close();

out.close();

out.flush();

}

catch(Exception e){

System.out.println("Exceptin "+ e);

}

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

But I didn't got anything in Tomcat console printed by the servlet. How is this caused and how can I solve it?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值