Forum Stats

  • 3,769,982 Users
  • 2,253,043 Discussions
  • 7,875,258 Comments

Discussions

How to use smallest time available?

Hayda
Hayda Member Posts: 2
edited Nov 24, 2016 4:59PM in New To Java

Hello

i found chat app , very simple and I want to add the exact time in nanosecond  or millisecond before sending and after receiving the message, so I add these lines

System.nanotime();

but I always got zero !!

Could you please help me, thanks

package androidclient.example.com.chatclient;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.Socket;

import java.net.UnknownHostException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import android.app.TimePickerDialog;

import android.net.Uri;

import android.os.AsyncTask;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import com.google.android.gms.appindexing.Action;

import com.google.android.gms.appindexing.AppIndex;

import com.google.android.gms.appindexing.Thing;

import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends Activity {

TextView textResponse, txt;

EditText editTextAddress, editTextPort;

Button buttonConnect, buttonClear;

EditText welcomeMsg;

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

private GoogleApiClient client;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextAddress = (EditText) findViewById(R.id.address);

editTextPort = (EditText) findViewById(R.id.port);

buttonConnect = (Button) findViewById(R.id.connect);

buttonClear = (Button) findViewById(R.id.clear);

textResponse = (TextView) findViewById(R.id.response);

txt = (TextView)findViewById(R.id.time) ;

welcomeMsg = (EditText) findViewById(R.id.welcomemsg);

buttonConnect.setOnClickListener(buttonConnectOnClickListener);

buttonClear.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

textResponse.setText("");

}

});

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

}

public OnClickListener buttonConnectOnClickListener = new OnClickListener() {

@Override

public void onClick(View arg0) {

String tMsg = welcomeMsg.getText().toString();

if (tMsg.equals("")) {

tMsg = null;

Toast.makeText(MainActivity.this, "No Welcome Msg sent", Toast.LENGTH_SHORT).show();

}

MyClientTask myClientTask = new MyClientTask(editTextAddress

.getText().toString(), Integer.parseInt(editTextPort

.getText().toString()),

tMsg);

myClientTask.execute();

txt.setText(Long.toString((myClientTask.res)/2));

}

};

/**

* ATTENTION: This was auto-generated to implement the App Indexing API.

* See https://g.co/AppIndexing/AndroidStudio for more information.

*/

public Action getIndexApiAction() {

Thing object = new Thing.Builder()

.setName("Main Page") // TODO: Define a title for the content shown.

// TODO: Make sure this auto-generated URL is correct.

.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))

.build();

return new Action.Builder(Action.TYPE_VIEW)

.setObject(object)

.setActionStatus(Action.STATUS_TYPE_COMPLETED)

.build();

}

@Override

public void onStart() {

super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

client.connect();

AppIndex.AppIndexApi.start(client, getIndexApiAction());

}

@Override

public void onStop() {

super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.

// See https://g.co/AppIndexing/AndroidStudio for more information.

AppIndex.AppIndexApi.end(client, getIndexApiAction());

client.disconnect();

}

public class MyClientTask extends AsyncTask<Void, Void, Void> {

String dstAddress;

int dstPort;

String response = "";

String msgToServer;

long sec1, sec2,res;

MyClientTask(String addr, int port, String msgTo) {

dstAddress = addr;

dstPort = port;

msgToServer = msgTo;

res = sec2- sec2;

}

@Override

protected Void doInBackground(Void... arg0) {

Socket socket = null;

DataOutputStream dataOutputStream = null;

DataInputStream dataInputStream = null;

try {

socket = new Socket(dstAddress, dstPort);

dataOutputStream = new DataOutputStream(

socket.getOutputStream());

dataInputStream = new DataInputStream(socket.getInputStream());

if (msgToServer != null) {

dataOutputStream.writeUTF(msgToServer);

//mToast.show();*/

}

long sec1 = System.nanoTime();

response = dataInputStream.readUTF();

long sec2 = System.nanoTime();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

response = "UnknownHostException: " + e.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

response = "IOException: " + e.toString();

} finally {

if (socket != null) {

try {

socket.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (dataOutputStream != null) {

try {

dataOutputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (dataInputStream != null) {

try {

dataInputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;

}

@Override

protected void onPostExecute(Void result) {

textResponse.setText(response);

super.onPostExecute(result);

}

}

}

Answers

  • Unknown
    edited Nov 24, 2016 10:19AM
    i found chat app , very simple and I want to add the exact time in nanosecond  or millisecond before sending and after receiving the message, so I add these linesSystem.nanotime();but I always got zero !!

    Correct - zero is EXACTLY what you should get if you subtract zero from zero.

    res = sec2- sec2;

    First you are subtracting 'sec2' from itself rather than subtracting 'sec1'.

    Second you are doing that subtraction, and setting 'res' to zero when you create the class instance.

    Third you set the values of 'sec1' and 'sec2' but never actually use them anywhere.

    long sec1 = System.nanoTime();response = dataInputStream.readUTF();long sec2 = System.nanoTime();

    Fourth you NEVER change to value of 'res' anywhere in the code.

    txt.setText(Long.toString((myClientTask.res)/2));

    So if that code executes at all it will use the value of 'res' from when it was set to zero.

  • Hayda
    Hayda Member Posts: 2
    edited Nov 24, 2016 4:59PM

    I laugh a lot on my self about this fault , but yes Im beginners and want to learn, thanks

    I did that

    myClientTask.execute();

    txt1.setText(Long.toString(sec1));

    txt2.setText(Long.toString(sec2));

    and remove res from class instance ....

    also I got zero??

This discussion has been closed.