修改
如果人们不认为这个问题足够清楚,那么请求一些明确而不是标记为诚实,我不确定如何提出这个问题!!
我正在尝试关注一些c#代码并转换为java以将数据发送到热敏打印机。我创建了一个名为processImageFiles的方法,这里我以字节数组的形式获取图像文件并按照我的意愿处理它们,这是我的方法:
public void processImageFiles(byte[] logoImage, int fileTypeId, int printOrder, Context context) throws IOException, InterruptedException {
switch (fileTypeId)
{
case 1:
break;
case 2:
LOGOS t_icon1 = new LOGOS();
LOGOS t_icon2 = new LOGOS();
LOGOS t_icon3 = new LOGOS();
LOGOS t_icon4 = new LOGOS();
LOGOS t_icon5 = new LOGOS();
LOGOS t_icon6 = new LOGOS();
switch (printOrder)
{
case 1:
t_icon1 = BitmapToByte(logoImage);
break;
case 2:
t_icon2 = BitmapToByte(logoImage);
break;
case 3:
t_icon3 = BitmapToByte(logoImage);
break;
case 4:
t_icon4 = BitmapToByte(logoImage);
break;
case 5:
t_icon5 = BitmapToByte(logoImage);
break;
case 6:
t_icon6 = BitmapToByte(logoImage);
break;
default:
break;
}
sendLogo(t_icon1,t_icon2,t_icon3,t_icon4,t_icon5,t_icon6, context);
}
在此我打电话给sendLogo:
sendLogo(下方):
public void sendLogo(LOGOS icon1, LOGOS icon2, LOGOS icon3, LOGOS icon4, LOGOS icon5, LOGOS icon6, Context context) throws IOException, InterruptedException {
byte[] temp1 = new byte[3];
byte[] temp2 = new byte[4];
temp1[0] = 0x1c;
temp1[1] = 0x71;
temp1[2] = 6;
sendToPrinter(temp1, context);
sendLogoPart(icon1, context);
sendLogoPart(icon2, context);
sendLogoPart(icon3, context);
sendLogoPart(icon4, context);
sendLogoPart(icon5, context);
sendLogoPart(icon6, context);
closeSocket(printerSocket);
}
然后我调用sendToPrinter:
public void sendToPrinter(final byte [] data, final Context context) throws IOException {
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(2000);
sharedPreferences = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
final String ipAddress = sharedPreferences.getString("IP", "null");
final int portNo = 9100;
if(printerSocket==null) {
printerSocket = new Socket(ipAddress, portNo);
}
DataOutputStream dOut = new DataOutputStream(printerSocket.getOutputStream());
dOut.writeInt(data.length);
dOut.write(data);
dOut.flush();
dOut.close();
Log.i("data sent"," to printer");
printedSuccessfully = true;
} catch (UnknownHostException e) {
e.printStackTrace();
Log.i("Unknown Host Exception Error: ", String.valueOf(e));
printedSuccessfully = false;
} catch (IOException e) {
e.printStackTrace();
Log.i("IO Exception Error: ", String.valueOf(e));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
和
sendLogoPart:
public void sendLogoPart(final LOGOS icon, final Context context) throws IOException {
final byte[] tmp = new byte[4];
tmp[0] = (byte)(icon.y >> 3); //4;
tmp[1] = 0x0;
tmp[2] = (byte)icon.x; //32;
tmp[3] = 0x0;
Context.MODE_PRIVATE);
}
这是徽标类
public static class LOGOS{
public int y;
public int x;
public byte[] logo;
}
运行时出现了几个问题,首先是在dOut.writeInt(data.length)的sendToPrinter方法中第一次出现nullpointer错误时调用sendLogoPart;
01-08 13:35:04.302 2598-2766/com.etlie.stockclient E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-252
Process: com.etlie.stockclient, PID: 2598
java.lang.NullPointerException
at com.etlie.stockclient.DBManager.Controller.Controller$26.run(Controller.java:1992)
一旦这导致应用程序崩溃,logcat继续产生与套接字相关的不同错误消息:
01-08 13:35:04.562 2598-2771/com.etlie.stockclient W/System.err﹕ java.net.SocketException: sendto failed: EPIPE (Broken pipe)
01-08 13:35:04.562 2598-2770/com.etlie.stockclient W/System.err﹕ java.net.SocketException: sendto failed: EPIPE (Broken pipe)
01-08 13:35:04.562 2598-2772/com.etlie.stockclient W/dalvikvm﹕ threadid=29: thread exiting with uncaught exception (group=0x41eddba8)
01-08 13:35:04.562 2598-2770/com.etlie.stockclient W/System.err﹕ at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
01-08 13:35:04.562 2598-2770/com.etlie.stockclient W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:468)
01-08 13:35:04.562 2598-2770/com.etlie.stockclient W/System.err﹕ at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
01-08 13:35:04.562 2598-2598/com.etlie.stockclient I/STOCK DOWNLOAD...﹕ PROCESSING
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at java.io.DataOutputStream.writeInt(DataOutputStream.java:180)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at com.etlie.stockclient.DBManager.Controller.Controller$26.run(Controller.java:1992)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at libcore.io.Posix.sendtoBytes(Native Method)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at libcore.io.Posix.sendto(Posix.java:156)
01-08 13:35:04.567 2598-2770/com.etlie.stockclient W/System.err﹕ at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
01-08 13:35:04.567 2598-2771/com.etlie.stockclient W/System.err﹕ libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
01-08 13:35:04.567 2598-2771/com.etlie.stockclient W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:468)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at java.io.DataOutputStream.writeInt(DataOutputStream.java:180)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at com.etlie.stockclient.DBManager.Controller.Controller$26.run(Controller.java:1992)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at at libcore.io.Posix.sendtoBytes(Native Method)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at libcore.io.Posix.sendto(Posix.java:156)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ at libcore.io.IoBridge.sendto(IoBridge.java:466)
01-08 13:35:04.572 2598-2771/com.etlie.stockclient W/System.err﹕ ... 5 more
01-08 13:35:04.572 2598-2770/com.etlie.stockclient W/System.err﹕ libcore.io.IoBridge.sendto(IoBridge.java:466)
1992年的线是第一个崩溃应用程序的线。如果有人可以帮助我,我会非常感激,因为我真的不知道问题是什么,sendToPrinter是否有多次调用以快速导致套接字问题