Question

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

package jvt_mplayer;

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

public class midlet extends MIDlet implements CommandListener {

private Display display;
 private Form form;
 private Command cQuit, cOk;
 private final String url="http://localhost:8080/getsongs.php";
 private String part;
 private TextField f;

 HttpConnection http;
 InputStream in;
  OutputStream out;
 int rc;
 private Command getPlaylist;

public void startApp() {
display = Display.getDisplay(this);
cQuit = new Command("Quit", Command.EXIT, 1);
form= new Form("midlet");
getPlaylist = new Command("Playlist", Command.OK, 1);
form.addCommand(cQuit);
form.addCommand(getPlaylist);
form.setCommandListener(this);
display.setCurrent(form);
}

public void processGet() throws Exception{
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
http.setRequestProperty("IF-Mofified-Since", "10 Nov 2006 17:29:12 GMT");
http.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
http.setRequestProperty("Content-Language", "en-US");

in = http.openDataInputStream();
out = http.openDataOutputStream();

rc = http.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
    throw new IOException("HTTP response code: " + rc);
}
System.out.println("Connected");
int ch;
StringBuffer buff = new StringBuffer();
while  ( (ch = in.read())!= -1){
buff.append( (char) ch);
}
form.append(new StringItem("Response: ", buff.toString()));

if (in != null)
    in.close();
if (out != null)
    out.close();
if (http != null)
    http.close();
}

public void commandAction(Command com, Displayable d){
if (com == cQuit){
    destroyApp(true);
    notifyDestroyed();
}
else if (com == getPlaylist){
    try{
        processGet();
    }
    catch(Exception er){
        System.out.println("Error in db access");
        er.printStackTrace();
    }
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

getsongs.php

<?php
echo "hi";
echo "jai";
?>

I need to establich connection between j2me and mysql .. i though of using php fr this .. I wrote a prototype to test if a j2me can fetch data from php script (code from a tutorial)

But i get this error :

javax.microedition.io.ConnectionNotFoundException: error 10061 in socket::open
at com.sun.midp.io.j2me.socket.Protocol.open0(), bci=0
at com.sun.midp.io.j2me.socket.Protocol.connect(), bci=209   
at com.sun.midp.io.j2me.socket.Protocol.open(), bci=216
at com.sun.midp.io.j2me.socket.Protocol.openPrim(), bci=4
at com.sun.midp.io.j2me.http.Protocol.createConnection(), bci=41

Any suggestions ?

Was it helpful?

Solution

hey firs its aways a good idea to put httpconnection inside a thread and when you open an inptustream you have to put it inside and try and catch

for me this code always has done the job

  Thread connection = new Thread(new Runnable(){

      public void run(){

      HttpConnection c;     
      InputStream is;
      OutputStream out;
      StringBuffer buff = new StringBuffer();
      String err = null;

      try{

            //Connecting to server passing the data and setting the connection property
            c = (HttpConnection)Connector.open(url);
            c.setRequestMethod(HttpConnection.GET);

           c.setRequestProperty("User-Agent","Profile/MIDP-2.1 Configuration/CLDC-1.1");
            c.setRequestProperty("If-Modified-Since","9 Oct 2012 17:49:31 GMT");
            c.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            c.setRequestProperty("Accept","text/html");


            //Read From the File

            try{

                is = c.openInputStream();

                //loop to read every character from file and append it to StringBuffer
                int ch;
                while((ch = is.read())  != -1){
                    buff.append((char) ch);
                }

                // now use the buffer will have al the data from php
            }catch(Exception e){

            }
        }catch(Exception e){
            display.setCurrent(text);
       }
    }
 });

connection.start();

i hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top