Boa tarde pessoal,
Fiz a seguinte aplicação abaixo (Client Socket ME e Socket Server SE). Com o emulador do toolkit (NetBeans) funciona legal, mas quando coloco num celular LG GT810H com Windows Mobile 6.1 conectado na minha rede via wireless acontece o seguinte erro quando tento criar conexão com o server: application not authorized to access the restricted api
Conto com a ajuda de vocês, porque já cansei de pesquisar a solução na net.
Obrigado. Abraços.
Server
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serverterminal;
import br.com.rtkomp.util.ConfigUtil;
import java.io.BufferedInputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Administrator
*/
public class Server {
private static int PORTA;
private static int TIMEOUT;
public Server() {
}
public static void main(String args[]) {
PORTA = Integer.parseInt(ConfigUtil.getProperties("porta_servidor"));
TIMEOUT = Integer.parseInt(ConfigUtil.getProperties("timeout"));
try {
ServerSocket scktServer = new ServerSocket(PORTA);
System.out.println("Serviço Iniciado, porta: " + PORTA);
while (true) {
String strMensagem = "";
Socket socket = scktServer.accept();
System.out.println("Requisição recebida de " + socket.getInetAddress().getHostAddress());
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] mensagem = new byte[1024];
int qtdBytes = bis.read(mensagem, 0, mensagem.length);
strMensagem = new String(mensagem, 0, qtdBytes);
int codigoMensagem = Integer.parseInt(strMensagem.substring(0, 4));
switch(codigoMensagem){
case 10 :
new LoginThread(socket, strMensagem).start();
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
// TODO implementar Log
}
}
}
....................................................
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package serverterminal;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class LoginThread extends Thread {
private Socket socket;
private String mensagem;
public LoginThread(Socket socket, String mensagem) {
this.socket = socket;
this.mensagem = mensagem;
}
public void run() {
System.out.println("Requisição recebida: "+mensagem);
BufferedOutputStream bos = null;
try {
String stringParse[] = mensagem.split(";");
mensagem = stringParse[1];
String resposta = String.valueOf(Double.parseDouble(mensagem) * 1.84);
bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(resposta.getBytes());
bos.flush();
System.out.println("Resposta enviada: " + resposta);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Aplicação ME
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Administrator
*/
public class ClientSocket extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private int tipoCmd;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
private Form frmMain;
private TextField tfdValor;
private ChoiceGroup cgpOpcao;
private StringItem siResultado;
private Alert altStatus;
private Command cmSair;
private Command cmConverter;
//</editor-fold>
/**
* The ClientSocket constructor.
*/
public ClientSocket() {
}
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
*/
private void initialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getFrmMain());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
/**
* Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == frmMain) {
if (command == cmConverter) {
tipoCmd = 0;
new ConnectionThread().start();
// write post-action user code here
} else if (command == cmSair) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmMain ">
/**
* Returns an initiliazed instance of frmMain component.
* @return the initialized component instance
*/
public Form getFrmMain() {
if (frmMain == null) {
// write pre-init user code here
frmMain = new Form("ConverterValores", new Item[] { getTfdValor(), getCgpOpcao(), getSiResultado() });
frmMain.addCommand(getCmSair());
frmMain.addCommand(getCmConverter());
frmMain.setCommandListener(this);
// write post-init user code here
}
return frmMain;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tfdValor ">
/**
* Returns an initiliazed instance of tfdValor component.
* @return the initialized component instance
*/
public TextField getTfdValor() {
if (tfdValor == null) {
// write pre-init user code here
tfdValor = new TextField("Valor", null, 32, TextField.ANY);
// write post-init user code here
}
return tfdValor;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cgpOpcao ">
/**
* Returns an initiliazed instance of cgpOpcao component.
* @return the initialized component instance
*/
public ChoiceGroup getCgpOpcao() {
if (cgpOpcao == null) {
// write pre-init user code here
cgpOpcao = new ChoiceGroup("Op\u00E7\u00F5es:", Choice.EXCLUSIVE);
cgpOpcao.append("Dollar para Real", null);
cgpOpcao.append("Real para Dollar", null);
cgpOpcao.setSelectedFlags(new boolean[] { false, false });
cgpOpcao.setFont(0, null);
cgpOpcao.setFont(1, null);
// write post-init user code here
}
return cgpOpcao;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: siResultado ">
/**
* Returns an initiliazed instance of siResultado component.
* @return the initialized component instance
*/
public StringItem getSiResultado() {
if (siResultado == null) {
// write pre-init user code here
siResultado = new StringItem("Resultado", "0");
// write post-init user code here
}
return siResultado;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmSair ">
/**
* Returns an initiliazed instance of cmSair component.
* @return the initialized component instance
*/
public Command getCmSair() {
if (cmSair == null) {
// write pre-init user code here
cmSair = new Command("Sair", Command.EXIT, 0);
// write post-init user code here
}
return cmSair;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmConverter ">
/**
* Returns an initiliazed instance of cmConverter component.
* @return the initialized component instance
*/
public Command getCmConverter() {
if (cmConverter == null) {
// write pre-init user code here
cmConverter = new Command("Converter", Command.SCREEN, 0);
// write post-init user code here
}
return cmConverter;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: altStatus ">
/**
* Returns an initiliazed instance of altStatus component.
* @return the initialized component instance
*/
public Alert getAltStatus() {
if (altStatus == null) {
// write pre-init user code here
altStatus = new Alert("alert");
altStatus.setTimeout(Alert.FOREVER);
// write post-init user code here
}
return altStatus;
}
//</editor-fold>
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
class ConnectionThread extends Thread {
public void run() {
String tempo = "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
if (tipoCmd == 0) {
SocketConnection sc;
try {
sc = (SocketConnection) Connector.open("socket://192.168.0.196:3009");
sc.setSocketOption(SocketConnection.LINGER, 5);
InputStream is = sc.openInputStream();
OutputStream os = sc.openOutputStream();
tempo = "Início: "+DataUtilME.getDataHora();
os.write(("0010;"+getTfdValor().getString()).getBytes());
byte[] retornoByte = new byte[1024];
int qtdBytes = is.read(retornoByte, 0, retornoByte.length);
String retornoStr = new String(retornoByte, 0, qtdBytes);
/*int ch = 0;
while (ch != -1) {
ch = is.read();
}*/
getSiResultado().setText(retornoStr);
tempo = tempo + " - Término: "+DataUtilME.getDataHora();
getAltStatus().setString(tempo);
System.out.println("Mensagem recebida: " + retornoStr);
is.close();
os.close();
sc.close();
new Thread().sleep(3000);
switchDisplayable(getAltStatus(), frmMain);
} catch (Exception ex) {
getAltStatus().setString("Erro ao converter valor: " + ex.getMessage());
switchDisplayable(getAltStatus(), getFrmMain());
}
}
}
}
}