×

Discussion Board

Results 1 to 6 of 6

Thread: pc + j2me

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    31

    pc + j2me

    hi
    i want to send textfile or picture form pc (server) to mobile (client) using jdk6 for server and j2me for client its possible or not to do this and how i can start to do this???????
    thx

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: pc + j2me

    Sure you can. If you want to use standard HTTP protocol, you'll find plenty of examples just searching this forums. Just a basic sample of accessing a txt file:

    http://www.java2s.com/Code/Java/J2ME/HttpConnection.htm

    Pit

  3. #3
    Regular Contributor
    Join Date
    May 2008
    Posts
    68

    Re: pc + j2me

    void getViaHttpConnection(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    int rc;

    try {
    c = (HttpConnection)Connector.open(url);

    // Getting the response code will open the connection,
    // send the request, and read the HTTP response headers.
    // The headers are stored until requested.
    rc = c.getResponseCode();
    if (rc != HttpConnection.HTTP_OK) {
    throw new IOException("HTTP response code: " + rc);
    }

    is = c.openInputStream();

    // Get the ContentType
    String type = c.getType();

    // Get the length and process the data
    int len = (int)c.getLength();
    if (len > 0) {
    int actual = 0;
    int bytesread = 0 ;
    byte[] data = new byte[len];
    while ((bytesread != len) && (actual != -1)) {
    actual = is.read(data, bytesread, len - bytesread);
    bytesread += actual;
    }
    } else {
    int ch;
    while ((ch = is.read()) != -1) {
    ...
    }
    }
    } catch (ClassCastException e) {
    throw new IllegalArgumentException("Not an HTTP URL");
    } finally {
    if (is != null)
    is.close();
    if (c != null)
    c.close();
    }
    }

  4. #4
    Regular Contributor
    Join Date
    May 2008
    Posts
    68

    Re: pc + j2me

    You have to check Content-Length also , some devices gives -1 as content length

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    31

    Re: pc + j2me

    hi all...
    iam try to receive textfile from java server to j2me client server send file correcte but the client dosen't show the file and idon't know where is the problem plz help me if you can thx this is my code

    server :

    import java.io.*;
    import java.net.*;
    import java.util.StringTokenizer;



    public class HttpServer {

    public static final int HTTP_PORT = 8080;

    public ServerSocket getServer() throws Exception {
    return new ServerSocket(HTTP_PORT);
    }


    public void run() {
    ServerSocket listen=null;
    try {
    listen = new ServerSocket(8080);
    while(true) {
    Socket client = listen.accept();
    System.out.println("Exception data recieved");
    ProcessConnection cc = new
    ProcessConnection(client);
    }
    } catch (IOException e) {
    System.out.println("Exception: "+e.getMessage());


    }
    }







    // main program
    public static void main(String argv[]) throws
    Exception {
    //HttpServer httpserver = new HttpServer();
    // httpserver.run();

    File transfer = new File("C://Users.txt");
    InputStream in = new FileInputStream(transfer);
    ServerSocket server = new ServerSocket(8080);
    Socket connection = server.accept();

    OutputStream output = connection.getOutputStream();

    byte[] buff = new byte[connection.getSendBufferSize()];
    int bytesRead = 0;

    System.out.println(transfer.length()+ " bytes");

    while((bytesRead = in.read(buff))>0)
    {
    output.write(buff,0,bytesRead);
    }
    in.close();
    server.close();
    connection.close();
    output.close();

    }
    }


    class ProcessConnection extends Thread {
    Socket client;
    BufferedReader is;
    DataOutputStream os;

    public ProcessConnection(Socket s) { // constructor
    client = s;
    try {
    // System.out.println("Exception data recieved");
    is = new BufferedReader(new InputStreamReader
    (client.getInputStream()));
    os = new DataOutputStream(client.getOutputStream());
    } catch (IOException e) {
    System.out.println("Exception: "+e.getMessage());
    }
    this.start(); // Thread starts here...this start()
    //will call run()
    }

    public void run() {
    try {
    // get a request and parse it.
    String request = is.readLine();
    System.out.println( "Request: "+request );

    client.close();

    } catch (Exception e) {
    System.out.println("Exception: " +
    e.getMessage());

    }
    }



    }


    client:
    import java.io.*;
    import javax.microedition.io.*;

    import javax.microedition.lcdui.*;

    public class GPRS
    {

    //public StringItem statusField;
    public GPRS()
    {

    }

    public void start ( )
    { //TCMS9 tcms = new TCMS9();
    HttpConnection connection = null;
    InputStream inputstream = null;

    try
    {
    //statusField.setText("AS");
    String data ="hello";
    String url="http://127.0.0.1:8080"+"?"+data;
    connection = (HttpConnection) Connector.open(url);
    //HTTP Request
    connection.setRequestMethod(HttpConnection.GET);
    connection.setRequestProperty("Content-Type","//text plain");
    connection.setRequestProperty("Connection", "close");
    // HTTP Response
    System.out.println("Status Line Code: " + connection.getResponseCode());
    System.out.println("Status Line Message: " + connection.getResponseMessage());
    if (connection.getResponseCode() == HttpConnection.HTTP_OK)
    {
    System.out.println(
    connection.getHeaderField(0)+ " " + connection.getHeaderFieldKey(0));
    System.out.println(
    "Header Field Date: " + connection.getHeaderField("date"));
    String str;
    inputstream = connection.openInputStream();
    int length = (int) connection.getLength();
    if (length != -1)
    {
    byte incomingData[] = new byte[length];
    inputstream.read(incomingData);
    str = new String(incomingData);
    }
    else
    {
    ByteArrayOutputStream bytestream =
    new ByteArrayOutputStream();
    int ch;
    while ((ch = inputstream.read()) != -1)
    {
    bytestream.write(ch);
    }
    str = new String(bytestream.toByteArray());
    bytestream.close();
    }
    System.out.println(str);
    }
    }
    catch(IOException error)
    {
    System.out.println("Caught IOException: " + error.toString());
    }
    finally
    {
    if (inputstream!= null)
    {
    try
    {
    inputstream.close();
    }
    catch( Exception error)
    {
    /*log error*/
    }
    }
    if (connection != null)
    {
    try
    {
    connection.close();
    }
    catch( Exception error)
    {
    /*log error*/
    }
    }
    }
    }
    }

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    31

    Exclamation Re: pc + j2me

    plz answer me

Similar Threads

  1. How can the WAP browser communicate to J2ME application?
    By hbfornies in forum Mobile Java General
    Replies: 20
    Last Post: 2007-03-02, 16:32
  2. J2ME Polish Version 1.0 RC6
    By enough in forum Mobile Java General
    Replies: 1
    Last Post: 2004-07-20, 11:25
  3. J2ME Polish 1.0 RC1!
    By enough in forum Mobile Java General
    Replies: 1
    Last Post: 2004-06-15, 02:53
  4. TinyLine SVG Minute Toolkit: Optimizing SVG for J2ME
    By girow in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2003-05-05, 18:44
  5. TinyLine SVG Minute Toolkit for J2ME: version 1.4 released
    By girow in forum Mobile Java General
    Replies: 0
    Last Post: 2003-05-05, 16:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×