thanks
i will show you the code , i have try to connect httpserver with
j2me .it works.the httpserver is running in my tomcat.
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;
}
thanks Alex
On 12月11日, 上午9时39分, Alex Tang <[email protected]> wrote:
> how is the header of httppost. is it correct?
>
> On Dec 11, 3:29 am, LeeMoon <[email protected]> wrote:
>
>
>
> > hi
> > is there someone troubled the same issue,please try to help me . i am
> > waiting for you , thanks
>
> > the code in run();//a thread class
> > reqEntity = new StringEntity(sendContent.toString());
> >
> > reqEntity.setContentType("application/x-www-form-urlencoded");
> > httppost.setEntity(reqEntity);
> > HttpResponse response =
> > httpclient.execute(httppost);
> > HttpEntity entity = response.getEntity();
> > if(entity != null) {
> > return entity.getContent();
> > }- 隐藏被引用文字 -
>
> - 显示引用的文字 -
--
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