I gave examples that I found on the Internet for socket programming, and I'm trying to create my own Android client for an Arduino server with Ethernet support. However, I have 2 problems. Firstly, the code for my main activity:
package com.domiflichi.TesterProject; import java.io.BufferedWriter; // output import java.io.BufferedReader; // input import java.io.InputStreamReader; // input import java.io.OutputStreamWriter; // output import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TesterProjectMain extends Activity implements OnClickListener { private Button connectPhones; private TextView myTextView; // represents the 'status text' private String serverIpAddress = ""; private boolean connected = false; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.client); connectPhones = (Button) findViewById(R.id.connect_phones); connectPhones.setOnClickListener(connectListener); connectPhones.setOnClickListener(this); myTextView = (TextView) findViewById(R.id.text1); } // This was created when using the 'implements OnClickListener' in the class public void onClick(View v) { if (v.getId() == R.id.connect_phones) { if (!connected) { serverIpAddress = "192.168.0.178"; if (!serverIpAddress.equals("")) { Thread cThread = new Thread(new ClientThread()); cThread.start(); connectPhones.setEnabled(false); // Once the button is pressed, disable it. :) } } } else if (v.getId() == R.id.status_req) { // CODE HERE FOR STATUS REQUEST BUTTON? } else if (v.getId() == R.id.cmd_toggle) { // CODE HERE FOR TOGGLE DOOR BUTTON? } else if (v.getId() == R.id.cmd_crack) { // CODE HERE FOR CRACK BUTTON? } else if (v.getId() == R.id.disconnect) { // CODE HERE FOR DISCONNECT BUTTON? } } public class ClientThread implements Runnable { public void run() { try { InetAddress serverAddr = InetAddress.getByName(serverIpAddress); Log.d("ClientActivity", "C: Connecting..."); Socket socket = new Socket(serverAddr, 23); connected = true; while (connected) { try { Log.d("ClientActivity", "C: Sending command."); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); out.println("mypass*"); Log.d("ClientActivity", "C: Sent."); BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream())); final StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); Log.d("Server response", line.toString()); } handler.post(new Runnable() { public void run() { if (total.toString().contentEquals("status:open")) { myTextView.setText(R.string.status_open); } else { myTextView.setText(R.string.status_closed); } } }); connected = false; } catch (Exception e) { Log.e("ClientActivity", "S: Error", e); } } socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { Log.e("ClientActivity", "C: Error", e); connected = false; } } } }
So my 2 questions:
I do not know how to make my buttons interact with the socket, where I send commands to my server. After I connected, I have (4) buttons that should send various commands through the socket (which runs in a separate thread, which was launched by pressing the "connect" button I have). (Look for the link "// CODE HERE FOR STATEMENT APPLICATION REQUIREMENTS?", Where did I expect to put the code for the various buttons)
I need to move
'handler.post (new Runnable () {'
block of code in my main loop:
(while ((line = r.readLine()) != null) { total.append(line); Log.d("Server response", line.toString()); }
However, when I do this and change:
if (total.toString().contentEquals("status:open")) {
to
if (line.toString().contentEquals("status:open")) {
(because I want to actually read only one line at a time), Eclipse complains about the following message: You cannot reference a non-finite variable string inside an inner class defined by another method.
And if I try to change the line above this ...: String; in a static line string;
Eclipse complains about the following line:
while ((line = r.readLine ())! = null) {
saying: The final local variable string may have already been assigned
I canβt even believe that I have reached the point that I am a complete beginner, but now I hit the wall.