Click here to Skip to main content
15,897,315 members
Home / Discussions / Java
   

Java

 
Questionmabye this question about netBeans(i dont' konw ) Pin
williamroma9-Apr-11 0:31
williamroma9-Apr-11 0:31 
AnswerRe: mabye this question about netBeans(i dont' konw ) Pin
williamroma9-Apr-11 1:13
williamroma9-Apr-11 1:13 
AnswerRe: mabye this question about netBeans(i dont' konw ) Pin
hyjiacan11-Apr-11 14:43
hyjiacan11-Apr-11 14:43 
Questionquestion Pin
fauz_m6-Apr-11 18:27
fauz_m6-Apr-11 18:27 
AnswerNot the answer Pin
Luc Pattyn7-Apr-11 1:51
sitebuilderLuc Pattyn7-Apr-11 1:51 
AnswerBut a suggestion Pin
Richard MacCutchan8-Apr-11 1:42
mveRichard MacCutchan8-Apr-11 1:42 
AnswerRe: question Pin
kurt110-May-11 9:27
kurt110-May-11 9:27 
Questionhow to recieve POST to localhost from J2ME Pin
williamroma2-Apr-11 2:15
williamroma2-Apr-11 2:15 
hi for all
(SORRY FOR MY BAD ENGLISH)
i am really thank you all because you always help me
today i am working in sending something from mobile using J2ME to localhost
i put the defalutUrl="http://localhost" is this right?
and how to the bytes (that i sent) to localhost ,
i read that the class "HttpServlet" and its functions is important to me is this right?
the most important thing is how to receive to localhost and should i make a php page to receive the bytes i sent?
this is the code
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author William
 */
public class midle extends MIDlet implements CommandListener {

    private static String defalutUrl="http://localhost";
    //GUI to input string form the user
    private Display myDisplay=null;
    private Form mainScreen;
    private TextField requestField;

    //GUI for displaying header information
    private Form resultScreen;
    private StringItem resultField;

    //the "SEND" button used in the mainScreen
    Command sendCommand = new Command("SEND", Command.OK, 1);

    //the "BACK" button used in the resultScreen
    Command backCommand = new Command("BACK", Command.OK, 1);

    public midle() {
        myDisplay=Display.getDisplay(this);
        mainScreen=new Form("Type in a string:");
        requestField=new TextField(null, "TOTTI ", 100, TextField.ANY);
        mainScreen.append(requestField);
        mainScreen.addCommand(sendCommand);
        mainScreen.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if(c==sendCommand){
            //retrieving the String that user had entered
            String requestString= requestField.getString();
            //sending a POST request to the web server
            String resultString= sendPostRequest(requestString);
            //displaying the response back from the web server
            resultScreen=new Form("result String");
            resultField=new StringItem(null,resultString);
            resultScreen.append(resultField);
            resultScreen.addCommand(backCommand);
            resultScreen.setCommandListener(this);
            myDisplay.setCurrent(resultScreen);
        }else{
            if(c==backCommand){
                //do it all again
                requestField.setString("SOMETHING GOOD");
                myDisplay.setCurrent(mainScreen);
            }else{
            }
        }
    }

    //send a POST request to the web server
    public String sendPostRequest(String requestString){
        HttpConnection hc=null;
        DataInputStream dis=null;
        DataOutputStream dos=null;
        StringBuffer messagebuffer=new StringBuffer();
        try {
            // open a http connection with the web server to send and recieve operation
            hc=(HttpConnection)Connector.open(defalutUrl,Connector.READ_WRITE);
            //set the request method to POST
            hc.setRequestMethod(HttpConnection.POST);
            //sent the string entered by user byte by byte
            dos=hc.openDataOutputStream();
            byte[] requst_budy=requestString.getBytes();
            for(int i=0;i<requst_budy.length;i++){
                dos.writeByte(requst_budy[i]);
            }
            dos.flush();
            dos.close();
            //retrieve the response back from the web server
            dis=new DataInputStream(hc.openInputStream());
            int ch;
            //first we have to check the content lenght
            long len=hc.getLength();
            if(len!=-1){
                for(int i=0;i<len;i++)
                    if((ch=dis.read())!=-1){
                        messagebuffer.append((char)ch);
                    }else{
                        while((ch=dis.read())!=-1){
                            messagebuffer.append((char)ch);
                        }
                    }
            }
            dis.close();
        } catch (IOException ioe) {
            messagebuffer.append("ERROR!");
        }finally{
            //free up i/0 streams and http connection
            try {
                if(hc!=null)
                    hc.close();
            } catch (Exception e) {
            }
            try {
                if(dis!=null)
                    dis.close();
            } catch (Exception e) {
            }
            try {
                if(dos!=null)
                    dos.close();
            } catch (Exception e) {
            }
            return messagebuffer.toString();
        }
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(mainScreen);
    }

}




thank you very much
AnswerRe: how to recieve POST to localhost from J2ME Pin
lakshr9-Nov-11 18:30
lakshr9-Nov-11 18:30 
Questionabout the NetBeans IDE Pin
williamroma31-Mar-11 23:36
williamroma31-Mar-11 23:36 
AnswerRe: about the NetBeans IDE Pin
Gerben Jongerius1-Apr-11 0:07
Gerben Jongerius1-Apr-11 0:07 
GeneralRe: about the NetBeans IDE Pin
williamroma1-Apr-11 13:40
williamroma1-Apr-11 13:40 
AnswerRe: about the NetBeans IDE Pin
David Skelly1-Apr-11 3:27
David Skelly1-Apr-11 3:27 
GeneralRe: about the NetBeans IDE Pin
williamroma1-Apr-11 13:40
williamroma1-Apr-11 13:40 
Questionsending sms from mobile to gmail inbox Pin
Member 779496629-Mar-11 0:37
Member 779496629-Mar-11 0:37 
AnswerRe: sending sms from mobile to gmail inbox Pin
Richard MacCutchan29-Mar-11 3:25
mveRichard MacCutchan29-Mar-11 3:25 
GeneralRe: sending sms from mobile to gmail inbox Pin
ankitjoshi246-Apr-11 9:35
ankitjoshi246-Apr-11 9:35 
AnswerRe: sending sms from mobile to gmail inbox Pin
RaviRanjanKr5-Apr-11 2:49
professionalRaviRanjanKr5-Apr-11 2:49 
Questionfirewall implementation Pin
Member 779496629-Mar-11 0:32
Member 779496629-Mar-11 0:32 
AnswerRe: firewall implementation Pin
Richard MacCutchan29-Mar-11 3:25
mveRichard MacCutchan29-Mar-11 3:25 
AnswerRe: firewall implementation Pin
TorstenH.29-Mar-11 20:03
TorstenH.29-Mar-11 20:03 
AnswerRe: firewall implementation Pin
ankitjoshi246-Apr-11 9:36
ankitjoshi246-Apr-11 9:36 
GeneralRe: firewall implementation Pin
akj26908-Apr-11 9:23
akj26908-Apr-11 9:23 
AnswerRe: firewall implementation Pin
JP_Rocks29-May-11 22:40
JP_Rocks29-May-11 22:40 
QuestionLarge source code repos Pin
Ozcan ILIKHAN28-Mar-11 19:33
Ozcan ILIKHAN28-Mar-11 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.