连接到servlet的问题来自Android客户端

时间:2011-03-24 05:39:05

标签: java android servlets httpconnection

我正在尝试连接到Servlet(Tomcat localhost)。这是我的代码。

ServletTest.java(客户)

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(服务器)

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

但是我没有在servlet打印的Tomcat控制台中得到任何东西。这是怎么造成的,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您没有执行HTTP请求。在写入请求正文并关闭流后,URLConnection将不会这样做。只有当您从响应中读取任何时,它才会执行。

在写入请求正文后执行此操作。例如。获得响应状态和/或正文。

int status = con.getResponseCode();

if (status == 200) {
    // Anything OK! Read if necessary response body. 
    // It'll contain whatever you wrote by response.getWriter() in servlet.
    InputStream response = con.getInputStream();
    // ...
}

另见:


此外,在Android上,您还需要设置用户连接Internet的权限。将以下内容添加到AndroidManifext.xml

<uses-permission android:name="android.permission.INTERNET" />

另见: