Ad

Data Transfer Between Android App On Device To PC Netbeans Java Server

- 1 answer

Good day, I am developing an android app to send textView value to a server made in Java netbeans, I followed a tutorial on YouTube [https://www.youtube.com/watch?v=29y4X65ZUwE ] however when I run the server first it does not send the data through. I am connected to the same Wi-Fi network as well.

EDIT: When I use the code System.out.println(ss.getInetAddress()); in my server class in Java, I get 0.0.0.0 as the ip address but I am connected to a wifi network.

Here is my AsyncTask class (written in android studio):

public class SendData extends AsyncTask<String, Void, Void> {

Socket s;
DataOutputStream dos;
PrintWriter pw;

@Override
protected Void doInBackground(String... voids) {

    String number = voids[0];
    try{

        s = new Socket("196.248.139.178", 6000);

        pw = new PrintWriter(s.getOutputStream());
        pw.write(number);
        pw.flush();
        pw.close();
        s.close();

    }catch(IOException ioe){

        ioe.printStackTrace();
    }
    return null;
  }
}

Here is my MainActivity(called it the Orders class, written in android studio):

public class Orders extends AppCompatActivity {

Button  send;
EditText orderNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orders);

    orderNum = findViewById(R.id.orderNum);

    send = findViewById(R.id.send);

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SendData numSender = new SendData();
            numSender.execute(orderNum.getText().toString());

        }
    });
  }
}

The following is my Server Code written in NetBeans, it includes a JFrame with a JTextArea to display to data sent from android phone:

public class OrderList extends javax.swing.JFrame {

static Socket s;
static ServerSocket ss;
static InputStreamReader isr;
static BufferedReader br;
static String numbers;


/**
 * Creates new form OrderList
 */
public OrderList() {

    initComponents();   
}
public static void main(String args[]) {

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new OrderList().setVisible(true);
        }
    });

     try{

         ss = new ServerSocket(6000);
         while(true){

             s = ss.accept();
             isr = new InputStreamReader(s.getInputStream());
             br = new BufferedReader(isr);
             numbers = br.readLine();

             System.out.println(numbers);
             // orderNumList is the text area where data is going to be set.
             if(orderNumList.getText().equals("")){

                 orderNumList.setText(numbers);
             }
             else{
                 orderNumList.setText(orderNumList.getText()+ "\n" + numbers);

             }

         }

     }catch(IOException e){

         e.printStackTrace();
     }
}

Any advice and help will be highly appreciated.

Ad

Answer

Try to read char by char because reading line by line can block. Check that : BufferedReader, detecting if there is text left to read

Ad
source: stackoverflow.com
Ad