Привет форумчане Может кто выручит?  Мои MIDlet Код | import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; import java.util.Vector;
public class MidletServlet extends MIDlet implements CommandListener { Display display = null; Form form = null; TextField tb = null; String str = null; String url = "http://localhost:8080/MyServer2/getText"; Command backCommand = new Command("Back", Command.BACK, 0); Command submitCommand = new Command("Submit", Command.OK, 2); Command exitCommand = new Command("Exit", Command.STOP, 3); private Test test; public MidletServlet() {} public void startApp() throws MIDletStateChangeException { display = Display.getDisplay(this); form = new Form("Request Servlet"); tb = new TextField("Please input text: ","",30,TextField.ANY ); form.append(tb); form.addCommand(submitCommand); form.addCommand(exitCommand); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable d) { if (c == exitCommand) { destroyApp(true); notifyDestroyed(); } else if (c == backCommand) { display.setCurrent(form); } else if (c == submitCommand) { str = tb.getString(); test = new Test(this); test.start(); test.getServlet(str); } } class Test implements Runnable { MidletServlet midlet; private Display display; String text; public Test(MidletServlet midlet) { this.midlet = midlet; display = Display.getDisplay(midlet); } public void start() { Thread t = new Thread(this); t.start(); } public void run() { StringBuffer sb = new StringBuffer(); try { HttpConnection c = (HttpConnection) Connector.open(url); c.setRequestProperty( "User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language","en-US"); c.setRequestMethod(HttpConnection.POST); DataOutputStream os = (DataOutputStream)c.openDataOutputStream(); os.writeUTF(text.trim()); os.flush(); os.close(); // Get the response from the servlet page. DataInputStream is =(DataInputStream)c.openDataInputStream(); //is = c.openInputStream(); int ch; sb = new StringBuffer(); while ((ch = is.read()) != -1) { sb.append((char)ch); } showAlert(sb.toString()); is.close(); c.close(); } catch (Exception e) { showAlert(e.getMessage()); } } /* This method takes input from user like text and pass to servlet */ public void getServlet(String text) { this.text = text; } /* Display Error On screen*/ private void showAlert(String err) { Alert a = new Alert(""); a.setString(err); a.setTimeout(Alert.FOREVER); display.setCurrent(a); } }; }
|
Мои Servlet Код | package test;
import java.io.DataInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class getText extends HttpServlet {
private static String string;
public void init() { }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FileReader fr; DataInputStream in = new DataInputStream((InputStream) request .getInputStream());
String text = in.readUTF(); try { fr = new FileReader("C:/test.txt"); int zeichen; while ( (zeichen = fr.read()) != -1) { string = new Character((char)zeichen).toString(); System.out.print( string ); } fr.close(); } catch (FileNotFoundException e) { System.out.println("Die Datei gibt's nicht!"); } catch(IOException e) { System.out.println("Es ist ein Lesefehler aufgetreten."); } String message = string; System.out.println(message); response.setContentType("text/plain"); response.setContentLength(message.length()); PrintWriter out = response.getWriter(); out.println(message); in.close(); out.close(); out.flush(); }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); } }
|
В Servlete вызывается FileReader и зачитывается txt-datei, к сожалению мой MIDlet получает обратно только первую букву из txt-datei, так как я зачитываю файл при помощи char.... Как можно зачитанные с char в while все знаки закинуть в один String, который я позже посылаю на мой MIDlet??? Вообщем мне надо все что находится в фаиле test.txt передать в мой MIDlet Это сообщение отредактировал(а) LeonLG - 27.11.2006, 17:07
|