0
   float totalKm =  Contsants.jobEndKm-Contsants.jobStartKm ;
    jcTotalKms.setText(String.format("%.2f",totalKm));
    //jcTotalKms.setText(Float.toString((float) (totalKm/16.0)));
    //finding total fare here
    //int value=100;
    if (totalKm<Contsants.minDist)
    {
        jcWaitingFare.setText("0");
        float totalfare=Contsants.minFare;
        jcTotalFare.setText(String.format("%.2f",(totalfare)));
        Contsants.jobTotalKm= totalKm;
        Contsants.jobTotalFare=totalfare;
    }
    else
    {
        jcWaitingFare.setText(Integer.toString((Contsants.cont_WaitingTimeInSec/60)*1));
        float totalfare= Contsants.minFare+ ((totalKm-Contsants.minDist) *Contsants.rupeeKm)  +(Contsants.cont_WaitingTimeInSec/60)*1;
        jcTotalFare.setText(String.format("%.2f",(totalfare)));
        Contsants.jobTotalKm= totalKm;
        Contsants.jobTotalFare=totalfare;
    }

tcpsocket 类

   public class tcpSocket extends Thread{
static boolean startSocket=false;
public static final String SERVERIP = "ip address here"; //your computer IP address
public static final int SERVERPORT = 8900;

private static  boolean mRun = false;
public static   Socket socket;
public static OnMessageReceived mMessageListener;

public static OnMessageReceived getmMessageListener() {
    return mMessageListener;
}
public static void setmMessageListener(OnMessageReceived mMessageListener) {
    tcpSocket.mMessageListener = mMessageListener;

}
public tcpSocket()
{

}
@Override
public void run(){
    //some long operation
    startSocket=true;
    while(startSocket)
    {
        try {
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);
            Log.d("TCP Client", "C: Connecting...");
            //create a socket to make the connection with the server
            socket = new Socket(serverAddr, SERVERPORT);
            try {
                //send the message to the server
                Log.d("TCP Client", "C: Sent.");
                Log.d("TCP Client", "C: Done.");
                final OutputStream out =socket.getOutputStream();
                writeResponse(out, "$0001~01~"+Contsants.Cont_IMEINo+"~Version#");
                //in this while the client listens for the messages sent by the server
                final InputStream in = socket.getInputStream();
                while(!mRun)
                {
                    if (!(in.available() > 0)) {
                        goSleep(2000);
                        continue;
                    }
                    processClient(in);
                }
            }catch (Exception e) {
                // TODO: handle exception
            }finally{
                socket.close();
            }
        } catch (Exception e) {
            Log.d("tcp error",e.toString());
            // TODO: handle exception
        }
    }
}
private   void goSleep(final long milliSec) {
    try {
        Thread.sleep(milliSec);
    } catch (final InterruptedException e) {
        Log.e("server conn Thread ","Sleeping client interrupted" + e);
    }
}
private void processClient(final InputStream in) throws IOException {
    final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    final char[] cbuf = new char[1024];
    final int length = reader.read(cbuf);
    if (length <= 0) {
        Log.d("d","No data read from client.");
        return;
    }
    cbuf[length] = '@';
    String packet   = new String(cbuf, 0, length + 1);
    if (!packet.startsWith("$") && !packet.contains("#")) {
        Log.d("d","Invalid packet recieved: " + packet);
        return;
    }
    try
    {
        if(!packet.contains("$0002") && !packet.contains("$0423"))
        {
            final String [] packetDo=packet.split("\\~");
            writeResponse(socket.getOutputStream(), "$102~"+packetDo[1]+"~"+Contsants.Cont_IMEINo+"#");
        }
        Log.d("d","Recived packet is :"+packet);

        Message msg = new Message();
        Bundle b = new Bundle();
        b.putString("ServerMsg", packet);
        msg.setData(b);
        // send message to the handler with the current message handler
        mHandler.sendMessage(msg);
    }catch (Exception e) {
        // TODO: handle exception
        Log.e("TCP", "p: Error", e);
    }
}

 Handler mHandler =new Handler(){
    @Override
    public void handleMessage(Message message){
        //update UI
        Bundle b = message.getData();
        final String data =b.getString("ServerMsg");
        mMessageListener.messageReceived(data);
    }
};
/*public class MyAsync extends AsyncTask<Void, Void, Boolean> {

    protected Boolean doInBackground(Void... params) {

        String response = null;
        return SendMessage(response);
    }

}

public static boolean SendMessage(final String response) {

    OutputStream out;
    try {
        out = socket.getOutputStream();
        writeResponse(out, response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        mRun = true;
        return false;
    }
    return true;
}
*/
public static boolean SendMessage(final String response) 
{
    OutputStream out;
    try {
        out = socket.getOutputStream();
        writeResponse(out,response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        mRun=true;
        return false;
    }
    return true;

}
private static  void writeResponse(final OutputStream out, final String response) throws IOException {
    // logger.info("Sending response to client: " + response);
    out.write(response.getBytes());
    out.flush();
}
 }

这是我在条件超过最小(minDist)公里时一起计算距离和票价的代码。但是在真实设备中检查此应用程序时,它会在两公里后挂断。因为两公里后它会进入其他条件部分。我不知道如何解决这个问题。

4

0 回答 0