一个带有j2me和php的函数

时间:2014-01-06 19:16:10

标签: php java-me

/ *  *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。  *要更改此模板文件,请选择“工具”|模板  *并在编辑器中打开模板。  * /

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";
?>

我需要建立j2me和mysql之间的连接..我虽然使用php fr这个..我写了一个原型来测试j2me是否可以从php脚本中获取数据(来自教程的代码)

但是我收到了这个错误:

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

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

嘿,将一个好的想法放在一个线程中是个好主意,当你打开一个inptustream时,你必须把它放在里面然后试着抓住

对我来说,这段代码总是完成这项工作

  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();

我希望这有帮助