J2ME i POST

0

nie rozumiem co z tym kodem przykładowym jest nie tak.. emulator telefonu pyta o polaczenie z internetem i jak klikam YES to nie zmienia sie ekran.. wreszcie wszedlem pod URL ten co jest w kodzie i okazalo sie ze tam juz nie stoi ten servlet. No dobra to stworzylem wlasny plik
http://kolos.math.uni.lodz.pl/~bart/skrypt.php
ktory ma zawartosc

<?php
var_dump($_POST);
?>

co powinno wywalic co sie postem wyslalo.. ale dalej to samo "Czy polaczyc z internetem o URL podanym...?" i YES nic nie zmienia.. nie wiem co jest grane

prosze o pomoc

oto kod:

package futbol24;

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.io.*; 
import java.io.*; 

public class Sample1 extends MIDlet implements CommandListener { 

	/* 
         * the default value for the URL string is * http://www.webyu.com/servlets/webyu/wirelessdevnetsample1 
         */ 

	private static String defaultURL = "http://www.webyu.com/servlets/webyu/wirelessdevnetsample1"; 

	// GUI component for user to enter String 
	private Display myDisplay = null; 
        private Form mainScreen; 
        private TextField requestField; 
        
	// GUI component for displaying header information 
        private Form resultScreen; 
	private StringItem resultField; 
        
	// the "SEND" button used on the mainScreen 
	Command sendCommand = new Command("SEND", Command.OK, 1); 
        
	// the "BACK" button used on the resultScreen 
	Command backCommand = new Command("BACK", Command.OK, 1); 

	public Sample1(){ 
		// initializing the GUI components for entering Web URL 
		myDisplay = Display.getDisplay(this); 
		mainScreen = new Form("Type in a string:"); 
		requestField = new TextField(null, "GREAT ARTICLE", 100, TextField.ANY); 
		mainScreen.append(requestField); 
		mainScreen.addCommand(sendCommand); 
		mainScreen.setCommandListener(this); 
	} 

	public void startApp() { 
		myDisplay.setCurrent(mainScreen); 
	} 

	public void pauseApp() { } 

	public void destroyApp(boolean unconditional) { } 

	public void commandAction(Command c, Displayable s) { 
		if (c == sendCommand) { 
			// retrieving the String that user entered 
			String requeststring = requestField.getString(); 
                        
			// sending a POST request to Web server 
			String resultstring = sendPostRequest(requeststring); 
                        
			// displaying the response back from 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 over again 
			requestField.setString("SOMETHING GOOD"); 
			myDisplay.setCurrent(mainScreen); 
		} 
	} 

	// send a POST request to Web server 
	public String sendPostRequest(String requeststring) { 

		HttpConnection hc = null; 
		DataInputStream dis = null; 
		DataOutputStream dos = null; 
		StringBuffer messagebuffer = new StringBuffer(); 

		try { 
                    // Open up a http connection with the Web server 
                    // for both send and receive operations 
                    hc = (HttpConnection) Connector.open(defaultURL, Connector.READ_WRITE); 
                    
                    // Set the request method to POST 
                    hc.setRequestMethod(HttpConnection.POST); 
                    
                    // Send the string entered by user byte by byte 
                    dos = hc.openDataOutputStream(); 
                    byte[] request_body = requeststring.getBytes(); 
                    
                    for (int i = 0; i < request_body.length; i++) { 
                        dos.writeByte(request_body[i]); 
                    } 
                    dos.flush(); 
                    dos.close(); 

                    // Retrieve the response back from the servlet 
                    dis = new DataInputStream(hc.openInputStream()); 
                    int ch; 
                    
                    // Check the Content-Length first 
                    long len = hc.getLength(); 
                    if(len!=-1) { 
                        for(int i = 0;i<len;i++) 
                            if((ch = dis.read())!= -1) {
                                messagebuffer.append((char)ch); 
                            } else { 
                                // if the content-length is not available 
                                while ((ch = dis.read()) != -1) 
                                messagebuffer.append((char) ch); 
                            }
                    }
                    
                    dis.close();
                } 
                catch (IOException ioe) { 
			messagebuffer = new StringBuffer("ERROR!"); 
		} 
                finally { 

			// Free up i/o streams and http connection 
			try { 
				if (hc != null) hc.close(); 
			} catch (IOException ignored) {} 
			try { 
				if (dis != null) dis.close(); 
			} catch (IOException ignored) {} 
			
			try { 
				if (dos != null) dos.close(); 
			} catch (IOException ignored) {} 
		} 
		
		return messagebuffer.toString(); 
	} 
} 
0

Proponuję sprawdzić to na telefonie, wygląda na to, że kod jest ok. Z jakiego emulatora korzystasz?

0

Sprawdziłem to; działa jeśli przerzucisz łączenie się do osobnego wątku.

0

korzystam z netbeans6
co oznacza przerzucic do osobnego watku, b. prosze prosciej..

1 użytkowników online, w tym zalogowanych: 0, gości: 1