Hi,
       I have written a code such that android client communicates
with servlets.That is username from android client is send to
servlets .And the username is printed(thru console)  after receiving
in servlets. But i am getting unknown host exception. Please help me
to fix this problem. Otherwise please post a sample code such that
android client communicates with servlets.
Please see the android client and servlet code below.

Android client
=================================================
package com.google.android.ServerSample;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ServerSample 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.login);

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

         Button goButton = (Button)findViewById(R.id.login);
         goButton.setOnClickListener(mGoListener);
    }

    private OnClickListener mGoListener = new OnClickListener()
    {
        public void onClick(View v)
        {
                String uname=username.getText().toString();
                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(){

                Log.v(TAG, "inside sub thread");

                try {
                URL ob = new URL("http://localhost:8084/SampleServlets/
TestServlet");
                URLConnection    conn = ob.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                OutputStream out = conn.getOutputStream();
                DataOutputStream dos =  new DataOutputStream(out);
                dos.writeInt(uname.getBytes().length);
        dos.write(uname.getBytes(),0,uname.getBytes().length);
        dos.flush();
        dos.close();
        conn = null;
                }catch(Exception e){
                        Log.v(TAG, "Exception:" +e);
                        e.printStackTrace();
                }
        }
}


====================================
Servlet code
===================================

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
    private InputStream is = null;
    private OutputStream os = null;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    boolean isValid = false;

    protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
        try{
            is = request.getInputStream();
            dis = new DataInputStream(is);
            int  len = dis.readInt();
            byte data[] = new byte[len];
            dis.read(data,0,len);
            String userName = new String(data);
            System.out.println("username:" +userName);
            is.close(); dis.close();
                   } catch(Exception e){
            System.out.println(e);
        }
    }

    protected void doGet(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    protected void doPost(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    public String getServletInfo() {
        return "Short description";
    }

}


==================

Thanks and Regards,
kannan


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to