📄 formsend.java
字号:
/**
* Class : short description
* @author Michele 'Miccar' Cardinale miccar@gmail.com
* @author Natale Vinto ebballon@interfree.it
* @version 1.0
*/
/*
* goText : text messaging over GPRS
* Copyright (C) 2006 Natale Vinto <ebballon@interfree.it>
* OpenJLab Group www.openjlab.org
* Copyright (C) 2006 Michele Cardinale 'Miccar' <miccar@gmail.com>
* www.miccar.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class FormSend extends Form implements CommandListener{
protected goText gotext;
protected Lang l;
protected Service s;
protected Message m;
protected StringItem si_status,si_perc,si_in,si_out;
private Command command_back;
public FormSend(goText gotext,Service s,Message m) {
super("");
this.gotext=gotext;
this.s=s;
this.m=m;
l=new Lang(gotext.lang);
this.setTitle(l.FS_SEND);
this.start();
}
private void start(){
String address=s.getAddress();
String datastring="";
datastring+="user="+s.getUser()+"&";
datastring+="pass="+s.getPass()+"&";
datastring+="nick="+utils.checkText(s.getNick())+"&";
datastring+="rcpt="+utils.checkText(m.getRecipients())+"&";
if(s.getSign().length()>0)
datastring+="text="+utils.checkText(m.getText()+"\n"+s.getSign());
else
datastring+="text="+utils.checkText(m.getText());
si_status=new StringItem(l.FS_STATUS, "Wait\n");
this.append(si_status);
si_perc=new StringItem(l.FS_PROGRESS, "0%\n");
this.append(si_perc);
si_out=new StringItem(l.FS_EBYTES, "0 bytes\n");
this.append(si_out);
si_in=new StringItem(l.FS_IBYTES, "0 bytes\n");
this.append(si_in);
new Thread(new Send(datastring, address)).start();
command_back=new Command(l.COMMAND_BACK,Command.BACK,2);
this.addCommand(command_back);
this.setCommandListener(this);
this.show();
}
public void show(){
gotext.display.setCurrent(this);
}
public void commandAction(Command c, Displayable d){
if (c==command_back){
gotext.mc.show();
}
}
class Send implements Runnable{
protected int perc=0;
protected String status;
protected String buffer="";
protected HttpConnection c;
protected InputStream is;
protected OutputStream os;
protected String datastring,address;
public Send(String datastring,String address){
this.datastring=datastring;
this.address=address;
}
public void run(){
String res="";
try{
res=connect();
}catch(GPRSException ge){
res="<num>-1</num><txt>"+ge.getMessage()+"</txt>";
}catch(ServiceException se){
res="<num>-2</num><txt>"+se.getMessage()+"</txt>";
}catch(Exception e){
res="<num>-3</num><txt>"+e.getMessage()+"</txt>";
}finally{
perc=100;
status="Complete";
this.CheckConnection();
processaRisposta(res);
}
}
/*
private String connect() throws Exception{
c=null;
is=null;
os=null;
perc=20;
status="Connecting";
this.CheckConnection();
if(gotext.options.getConnectionMethod()==0){ //POST METHOD SELECTED
byte[] data = datastring.getBytes();
try {
c = (HttpConnection)Connector.open(address,Connector.READ_WRITE);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Content-Length",String.valueOf(data.length));
c.setRequestProperty("Connection", "close");
} catch(Exception e){
throw new GPRSException("Starting connection error");
}
perc=40;
status="Writing";
this.CheckConnection();
try{
os = c.openOutputStream();
os.write(data);
//os.flush();
} catch(Exception e){
throw new GPRSException("Writing data error");
} finally{
if (os != null){
try {
os.close();
} catch (Exception e) {}
}
}
} else { //GET METHOD SELECTED
address=address+"?"+datastring;
address=utils.replaceString(address, " ", "%20");
address=utils.replaceString(address, "\n","%0A");
try {
c = (HttpConnection)Connector.open(address);
//c.setRequestProperty("Connection", "close");
} catch(Exception e){
throw new GPRSException("Starting connection error");
}
}
perc=60;
status="Reading";
this.CheckConnection();
try{
int rc=c.getResponseCode();
if (rc==200) {
try{
is = c.openInputStream();
int ch;
while ((ch = is.read()) != -1) {
buffer=buffer+( (char)ch );
}
}catch(Exception ee){
throw new GPRSException("Reading data error ("+ee.getMessage()+")");
}
if (utils.getTag(buffer, "txt").equals(""))
throw new ServiceException("Response content error");
} else {
throw new ServiceException("Response code error ("+rc+")");
}
}catch (Exception e) {
throw e;
}finally {
if (is != null){
try {
is.close();
} catch (Exception e) {}
}
}
perc=80;
status="Closing";
this.CheckConnection();
if (c != null){
try{
c.close();
} catch (Exception e){}
}
return buffer;
}
*/
private String connect() throws Exception{
try{
c=null;
is=null;
os=null;
perc=20;
status="Connecting";
this.CheckConnection();
if(gotext.options.getConnectionMethod()==1){ //GET METHOD SELECTED
address=address+"?"+datastring;
address=utils.replaceString(address, " ", "%20");
address=utils.replaceString(address, "\n","%0A");
try {
c = (HttpConnection)Connector.open(address);
//c.setRequestProperty("Connection", "close");
} catch(Exception e){
throw new GPRSException("Starting connection error (GET): "+e.toString());
}
} else { //POST METHOD SELECTED
byte[] data = datastring.getBytes();
try {
c = (HttpConnection)Connector.open(address,Connector.READ_WRITE);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Content-Length",String.valueOf(data.length));
c.setRequestProperty("Connection", "close");
} catch(Exception e){
throw new GPRSException("Starting connection error (POST): "+e.toString());
}
perc=40;
status="Writing";
this.CheckConnection();
try{
os = c.openOutputStream();
os.write(data);
//os.flush();
} catch(Exception e){
throw new GPRSException("Writing data error: "+e.toString());
}
}
perc=60;
status="Reading";
this.CheckConnection();
try{
int rc=c.getResponseCode();
if (rc==200) {
try{
is = c.openInputStream();
int ch;
while ((ch = is.read()) != -1) {
buffer=buffer+( (char)ch );
}
}catch(Exception ee){
throw new GPRSException("Reading data error: "+ee.getMessage());
}
if (utils.getTag(buffer, "txt").equals(""))
throw new ServiceException("Response content error (no TXT tag)");
} else {
throw new ServiceException("Response code error ("+rc+")");
}
}catch (Exception e) {
throw e;
}
} catch (Exception e){
throw e;
} finally {
perc=80;
status="Closing";
this.CheckConnection();
if (os != null){
try {
os.close();
} catch (Exception e) {}
}
if (is != null){
try {
is.close();
} catch (Exception e) {}
}
if (c != null){
try{
c.close();
} catch (Exception e){}
}
}
return buffer;
}
private void CheckConnection(){
FormSend.this.si_status.setText(status+"\n");
FormSend.this.si_perc.setText(perc+"%\n");
FormSend.this.si_out.setText(datastring.length()+" bytes\n");
FormSend.this.si_in.setText(buffer.length()+" bytes\n");
if (FormSend.this.isShown()){
FormSend.this.show();
}
}
/**
*
* @param stringa
*/
private void processaRisposta(String stringa){
int num;
String txt="";
try{
num=Integer.parseInt(utils.getTag(stringa, "num"));
}catch(Exception e){
num=-1;
}
txt=utils.getTag(stringa, "txt");
FormSend.this.append(txt);
FormSend.this.show();
switch(num){
case -2:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_ANSWER, gotext, AlertType.ERROR);
break;
case -1:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_GPRS, gotext, AlertType.ERROR);
break;
case 0:
s.increaseSentMsg(m.getRecipientsArray().length);
gotext.options.increaseCounter(m.getRecipientsArray().length);
utils.popup(l.ALERT_INFO_TITLE, l.FS_TEXT_SENT, gotext, AlertType.INFO);
break;
case 1:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_DATA, gotext, AlertType.ERROR);
break;
case 2:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_LOGIN, gotext, AlertType.ERROR);
break;
case 3:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_WEBSITE+" ("+s.getName()+")", gotext, AlertType.ERROR);
break;
default:
utils.popup(l.ALERT_ERROR_TITLE, l.FS_ERROR_UNKNOW+": "+num, gotext, AlertType.ERROR);
break;
}
}
public class ServiceException extends Exception{
public ServiceException(String message){
super(message);
}
}
public class GPRSException extends Exception{
public GPRSException(String message){
super(message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -