×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    newbie : code 404 when trying to connect to localhost/test.txt

    Hello,

    i really need help with the http cconnection. i use the code from the complete reference j2me (p.604) with some addition commands (thread), coz i always get the "to avoid deadlock). i get the tip from --> http://discussion.forum.nokia.com/fo...p/t-78705.html

    but now i have a problem when i try to connect to my localhost.
    i always get the 404 response code. as far as i know, 404 code is the code for file not found. but i have put my test.txt in htdocs\

    here's what i used:
    1. apache2triad
    Server running Apache/2.0.55 (Win32) PHP/4.4.2
    Server bound on 127.0.0.1 : 80
    Server admin addr admin@localhost
    Server webroot C:/apache2triad/htdocs
    Local Time 10:03:33 AM Tuesday 25.09.2007
    Logged in user root
    Client proxy (none)
    Client address 127.0.0.1
    Client port 1958
    Access server via localhost

    2. jwtoolkit2.5.1


    thx before


    note : i have disabled my pc's firewall
    n here's my code:
    ****************************
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.io.*;

    public class TestConnection extends MIDlet implements CommandListener{
    private Command exit, start;
    private Display display;
    private Form form;

    public TestConnection(){
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.EXIT, 1);
    start = new Command("Start", Command.EXIT, 1);
    form = new Form("Http Connection");
    form.addCommand(exit);
    form.addCommand(start);
    form.setCommandListener(this);
    }

    public void startApp(){
    display.setCurrent(form);
    }

    public void pauseApp(){
    }

    public void destroyApp(boolean unconditional){

    }

    public void commandAction(Command command, Displayable displayable){
    if (command == exit){
    destroyApp(false);
    notifyDestroyed();
    }else if (command == start) {
    Thread httpsThread = new Thread(){
    public void run() {

    HttpConnection connection = null;
    InputStream inputstream = null;
    try{
    connection = (HttpConnection) Connector.open("http://localhost/test.txt");
    //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());
    System.out.println("Status Line Http Connection: " + HttpConnection.HTTP_OK);
    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);
    }else { System.out.println("FAIL");}
    }

    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*/
    }
    }
    }
    } };httpsThread.start();

    }
    }

    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: newbie : code 404 when trying to connect to localhost/test.txt

    What if you try to access http://localhost/ ?Will it show some error even if you don't specify a file?


    Also, you could try to use "127.0.0.1" instead "localhost".I mean,

    http://127.0.0.1/test.txt

    instead

    http://localhost/test.txt

    maybe it'll work, maybe it won't


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Re: newbie : code 404 when trying to connect to localhost/test.txt

    than you for the reply ..
    i try 127.0.0.1/test.txt
    it works

    i also try http://localhost without anyfile added .. n it works also :

    Running with storage root DefaultColorPhone
    Running with locale: English_United States.1252
    Running in the identified_third_party security domain
    Status Line Code: 200
    Status Line Message: OK
    Status Line Http Connection: 200
    WTK OTA server server
    Header Field Date: Tue Sep 25 12:09:13 GMT+07:00 2007

    but if i added test.txt it failed again
    ??

    will it be a problem if i use my application in real device, if i use url instead of ip??

    i gues for a momentary, i'll use the ip ..
    now i'll go to my next-to-do that is to connect my mobile application to my php file in localhost
    thanks alb

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: newbie : code 404 when trying to connect to localhost/test.txt

    Quote Originally Posted by lynx3521 View Post
    ...will it be a problem if i use my application in real device, if i use url instead of ip??

    i gues for a momentary, i'll use the ip ...
    "127.0.0.1" is just the IP of "localhost": they are the same thing, but some phones seem to not understand you're trying to connect to a local host when you specify "localhost" instead the IP.
    I would say using "127.0.0.1" will always give you a better compatibility, as any phone that can connect to a local host will understand what "127.0.0.1" is.


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

Similar Threads

  1. Emulated Nokia phone can not connect to bluegiga WT12 module
    By hwan122 in forum Bluetooth Technology
    Replies: 0
    Last Post: 2007-01-09, 01:05
  2. Replies: 0
    Last Post: 2003-02-24, 04:00
  3. 6510 wallet code resets itself
    By o00ze in forum Mobile Commerce
    Replies: 1
    Last Post: 2003-02-05, 09:29
  4. Help! Basic TCP connection - Source code attached
    By dineshguru in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2002-10-10, 05:01
  5. problem with Nokia Toolkit 3.1 and binary code for MMS
    By alinesimon in forum General Messaging
    Replies: 1
    Last Post: 2002-08-09, 06:40

Posting Permissions

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