【问题标题】:A function with j2me and php一个带有 j2me 和 php 的函数
【发布时间】:2014-01-07 05:20:24
【问题描述】:

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

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

有什么建议吗?

【问题讨论】:

    标签: php java-me


    【解决方案1】:

    嘿,把 httpconnection 放在一个线程中是个好主意,当你打开一个 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();
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多