I wanna develop a application in Android to connet HttpServer.
But there is  something must be wrong.so if anyone read it or have
advice ,please tell me.I was troubled by this issue.

i am a green hand in Android , so careful and prolixity,please be
patient. If you are happy to give my opinion I will be very grateful,
if you do not mind, I will often ask your advice.thank you.


part 1 :...i have created two TextView  to input userid and
password;so i try to get the InpustStream
    code:
            switch(v.getId()) {
                case R.id.ok:
             //the code of  class PackMessage show you later
                        PackMessage pm = new PackMessage();
           //sendContent to get the message that have packed
                        sendContent = pm.login(id.getText().toString(), 
pas.getText
().toString());
          //let sm get packedm.toString
                        SendMessage sm = new 
SendMessage(sendContent.toString());
          //the class SendMessage have implemented Runnable
                        Thread smthread = new Thread(sm);
         //so turn to method run in class SendMessage
                        smthread.start();
                        break;

                case R.id.cancel:
                        finish();
                        break;
           }



part2: this is the code of class PackMessage


                public class PackMessage {
        public StringBuffer  login(String userid,String password)
        {

                       //put the input from in sb

                StringBuffer sb = new StringBuffer();
                sb.append("<purpose>login</purpose>");
                sb.append("<userid>"+userid+"</userid>" );
                sb.append("<password>"+password+"</password>");
                return sb;

        }

}

part3:    the constructor of  class SendMessage

                       public SendMessage(String sendContent)
        {
                SendMessage.sendContent = sendContent;
        }

part4:   the code of   run();

                public void run() {

                getHttpStream();
                Intent Intent = new Intent();
// that is the point :getHttpStram();
                Intent.putExtra("id", getHttpStream().toString());
                Intent.setAction("com.google.lm.Main");
                startActivity(Intent);

        }



part5 : code of getHttpStram();and the


public static InputStream getHttpStream() {
                HttpPost httppost = new 
HttpPost("http://localhost:8080/J2ME/servlet/
HelloServlet");
                DefaultHttpClient httpclient = new DefaultHttpClient();
                StringEntity reqEntity;
                try {
                        reqEntity = new StringEntity(sendContent.toString());
                        
reqEntity.setContentType("application/x-www-form-urlencoded");
                        httppost.setEntity(reqEntity);
                 //always catched IOException when ------
httpclient.execute(httppost);
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        if(entity != null) {
                                return entity.getContent();
                        }
                }
                catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                catch (IllegalStateException e) {
                        e.printStackTrace();
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }
}

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

Reply via email to