The list is being sent to the server. Its just that the server is not
able to receive the list and print it out.
PS: The MyFriend class implements Serializable

On Apr 10, 1:00 am, raqz <[email protected]> wrote:
> Hi,
>
> I am trying to send a list from the android device to a webserver
> which will receive the list and send it back to the android device. I
> have written the below code and it runs fine if I just send values but
> when I send an object, the app freezes. Could someone please help me
> out in this.
>
> Thanks
>
> Code for client
>
> package com.httpurl;
>
> import java.io.BufferedReader;
> import java.io.InputStreamReader;
> import java.io.ObjectOutputStream;
> import java.io.OutputStreamWriter;
> import java.net.URL;
> import java.net.URLConnection;
> import java.util.ArrayList;
> import java.util.List;
> import java.util.StringTokenizer;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.widget.TextView;
> import android.widget.Toast;
>
> public class MainActivity extends Activity {
>         public static List<MyFriend> listOfFriends = new
> ArrayList<MyFriend>();
>
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         getFriendsList();
>         getconnection(listOfFriends);
>         setContentView(R.layout.main);
>         TextView tv = new TextView(this);
>         tv.setText("hello");
>         setContentView(tv);
>
>     }
>
>     public void getFriendsList() {
>                 String FName =null;
>                 String LName =null;
>                 String Latitude =null;
>                 String Longitude =null;
>                 String TimeStamp =null;
>
>                 String line[] = new String[3];
>                 line[0]= "Al#Rb#23.44#34.44#4.45";
>                 line[1]= "Al#Az#33.44#44.44#5.45";
>                 line[2]= "Kl#Az#53.44#454.44#6.45";
>                 for(int k=0;k<=2;k++){
>                 StringTokenizer tokens=new StringTokenizer(line[k],"#");
>             try{
>                 while(tokens.hasMoreTokens())
>             {
>                 FName=tokens.nextToken();
>                 LName=tokens.nextToken();
>                 Latitude=tokens.nextToken();
>                 Longitude=tokens.nextToken();
>                 TimeStamp=tokens.nextToken();
>                 listOfFriends.add(new
> MyFriend(FName,LName,Latitude,Longitude,TimeStamp));
>             }
>             }
>             catch (Exception e){
>                 Toast.makeText(this,
>                                "Some prob here:"+e.getLocalizedMessage(),
>                                     Toast.LENGTH_SHORT).show();
>                 e.printStackTrace();
>             }
>                 }
>
> }
>
>         private void getconnection(List<MyFriend> list) {
>
>                 try{
>                         URL url = new 
> URL("http://10.0.2.2/HelloServlet/NewServlet";);
>                     URLConnection conn = url.openConnection();
>                     conn.setDoOutput(true);
>
>                     Toast.makeText(getBaseContext(),
>                     "connection set",
>                     Toast.LENGTH_SHORT).show();
>                     //OutputStreamWriter writer = new
> OutputStreamWriter(conn.getOutputStream());
>                     ObjectOutputStream out = new
> ObjectOutputStream(conn.getOutputStream());
>                     out.writeObject(list);
>
>                     //writer.write("value=hello&anotherValue=how r u");
>                     out.flush();
>                     Toast.makeText(getBaseContext(),
>                     "sent the list",
>                     Toast.LENGTH_SHORT).show();
>                     String line;
>                     BufferedReader reader = new BufferedReader(new
> InputStreamReader(conn.getInputStream()));
>                     while ((line = reader.readLine()) != null) {
>                       System.out.println(line);
>                       Toast.makeText(getBaseContext(),
>                       "received from server"+line,
>                       Toast.LENGTH_SHORT).show();
>                     }
>                     out.close();
>                     reader.close();
>
>                 }
>                 catch (Exception e){
>
>                 }
>
>         }
>
> }
>
> The Server code is
> protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
>     throws ServletException, IOException, ClassNotFoundException {
>         response.setContentType("text/html;charset=UTF-8");
>         PrintWriter out = response.getWriter();
>         ObjectInputStream ois = new
> ObjectInputStream(request.getInputStream());
>         List<MyFriend> list = (List<MyFriend>) ois.readObject();
>         Iterator itr = list.iterator();
>         while(itr.hasNext()){
>             MyFriend obj = (MyFriend) itr.next();
>             out.println("recived:"+obj.getFName());
>         }
>         String value2 = request.getParameter("anotherValue");
>         try {
>
>             //out.println("<html>");
>             //out.println("<head>");
>             //out.println("<title>Servlet NewServlet</title>");
>             //out.println("</head>");
>             //out.println("<body>");
>             //out.println("<h1>Servlet NewServlet at " +
> request.getContextPath () + "</h1>");
>             //out.println("recived"+value+" nextvalue"+value2);
>             //out.println("</body>");
>             //out.println("</html>");
>
>         } finally {
>             out.close();
>         }
>     }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" 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-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to