我只是想将String文本从我的Android应用发送到Nodemcu esp8266,并用其他文本发送给esp8266响应。
我可以在esp8266上收到文本,但是在android应用上我没有收到任何响应文本!而且我不知道出了什么问题。
我的android代码是
public class MainActivity extends Activity {
TextView textResponse;
Button buttonConnect;
EditText welcomeMsg;
String IpAddress = "192.168.0.117";
int Port = 8090;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonConnect = (Button) findViewById(R.id.connect);
textResponse = (TextView) findViewById(R.id.response);
welcomeMsg = (EditText)findViewById(R.id.welcomemsg);
buttonConnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
MyClientTask myClientTask = new MyClientTask(welcomeMsg.getText().toString());
myClientTask.execute();
}
});
}
@SuppressLint("StaticFieldLeak")
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String response = "";
String msgToServer;
MyClientTask(String msgTo) {
msgToServer = msgTo;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket(IpAddress, Port);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
if(!msgToServer.equals(""))
dataOutputStream.writeUTF(msgToServer + "$");
response = dataInputStream.readUTF();
} catch (IOException e) { }
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
}
我的esp8266代码是
#include <ESP8266WiFi.h>
WiFiServer server(8090);
IPAddress ip(192, 168, 0, 117);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
WiFi.config(ip, gateway, subnet);
WiFi.begin("TP-LINK", "55555555");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
server.begin();
Serial.println("Connected");
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println(client.readStringUntil('$'));
String x = "esp8266";
client.println(x);
delay(100);
client.flush();
}
这就是我的应用锁定方式
有人有什么主意吗?
答案 0 :(得分:0)
使用下面的代码发送数据:
示例:sendData(“ http://192.168.0.117:8090/data”);
private void sendData(final String requestURL) {
reply = "";
new AsyncTask<Object, Void, String>() {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Object... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(requestURL);
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputstream = entity.getContent();
BufferedReader bufferedreader =
new BufferedReader(new InputStreamReader(inputstream));
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
while ((currentline = bufferedreader.readLine()) != null) {
stringbuilder.append(currentline + "\n");
}
String result = stringbuilder.toString();
reply = result;
inputstream.close();
}
} catch (NetworkOnMainThreadException ne) {
String err = (ne.getMessage() == null) ? "Network" : ne.getMessage();
reply = err;
} catch (MalformedURLException me) {
String err = (me.getMessage() == null) ? "Malform" : me.getMessage();
reply = err;
} catch (ProtocolException pe) {
String err = (pe.getMessage() == null) ? "Protocol" : pe.getMessage();
reply = err;
} catch (IOException ioe) {
String err = (ioe.getMessage() == null) ? "IOError" : ioe.getMessage();
reply = err;
}
return reply;
}
@Override
protected void onPostExecute(String result) {
Log.v(reply);
}
}.execute();
}