Android - 读取 http 请求 [英] Android - Reading http request
问题描述
我正在使用 Arduino 和 Android 应用程序做一个项目.这是怎么回事,从应用程序请求发送a=11&b=22&d=33"(http://192.168.0.17/?a=11&b=22&d=33),arduino 读取并返回Data_received",我用它来做一些事情(向下是来自应用程序的部分代码).一切进展顺利.但是我现在不知道如何反转,我知道如何将请求从 arduino 发送到服务器,但我不知道如何在 android 应用程序中接收以获取 a = 11、b = 22、c = 33.要接收的 Android Studio 代码:
I'm doing a project with Arduino and Android application. Here's how it goes, send from the application request "a=11&b=22&d=33" (http://192.168.0.17/?a=11&b=22&d=33), arduino read and return "Data_received" and I use it to do something (down is part of code from app). And everything is going well. But I do not know how to do reversed now, I know how to send the request from arduino to server, but I don't know how to receive in android app to get a = 11, b = 22, c = 33. Android studio code to receive:
@Override
protected void onPostExecute(String s) {
if (serverResponse.toString().equals("Data_received") ){
Toast.makeText(Test.this, "Data is ok...", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Test.this, "Connection is not ok", Toast.LENGTH_SHORT).show();
}
}
在arduino中接收请求的Arduino代码:
Arduino code which receives request in arduino:
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int a;
int b;
int d;
char *pch;
while (client.connected()) {
while (client.available()) {
char c = client.read();
if (currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
while (client.available()) client.read();
Serial.print (tBuf);
pch = strtok(tBuf, "?");
while (pch != NULL) {
// http://192.168.0.17/?a=11&b=22&d=33
if (strncmp(pch, "a=", 2) == 0) {
a = atoi(pch + 2);
Serial.print("a=");
Serial.println(a, DEC);
}
if (strncmp(pch, "b=", 2) == 0) {
b = atoi(pch + 2);
Serial.print("b=");
Serial.println(b, DEC);
}
if (strncmp(pch, "d=", 2) == 0) {
d = atoi(pch + 2);
Serial.print("d=");
Serial.println(d, DEC);
}
pch = strtok(NULL, "& ");
} // while (pch != NULL)
client.stop();
}
}
}
所有代码:
公共类测试扩展 AppCompatActivity {
public class Test extends AppCompatActivity {
public EditText text__T1mod;
public EditText text__T2mod;
public EditText text__T3mod;
private TextView Text_Arduino;
private Button SendRequest ;
public int a;
public int b;
public int c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
SendRequest = (Button) findViewById(R.id.btn_SendRequest );
text__T1mod = (EditText) findViewById(R.id.TextEdit_T1);
text__T2mod = (EditText) findViewById(R.id.TextEdit_T2);
text__T3mod = (EditText) findViewById(R.id.TextEdit_T3);
Text_Arduino = (TextView) findViewById(R.id.textView_Arduino);
} // protected void onCreate END
public void clik_SendRequest (View view) {
String RequestString = "";
if (IpAddres.getText().toString().equals(""))
Toast.makeText(this, "Enter IP", Toast.LENGTH_SHORT).show();
else {
if (view == SendRequest )
// http://192.168.1.14/?a=11&b=22&d=33
RequestString = "a=" + text__T1mod.getText().toString() + "&"
+ "b=" + text__T2mod.getText().toString() + "&"
+ "d=" + text__T3mod.getText().toString() ;
// 192.168.1.14 // 80
String serverAdress = IpAddres.getText().toString() + ":" + Port.getText().toString() + "?";
HttpRequestTask requestTask = new HttpRequestTask(serverAdress);
requestTask.execute(RequestString);
} // else END
} // clik_SendRequest END
private class HttpRequestTask extends AsyncTask<String, Void, String> {
private String serverAdress;
private String serverResponse = "";
private AlertDialog dialog;
public HttpRequestTask(String serverAdress) {
this.serverAdress = serverAdress;
dialog = new AlertDialog.Builder(context)
.setTitle("HTTP zahtev za Ip adresu: ")
.setCancelable(true)
.create();
}
@Override
protected String doInBackground(String... params) {
String val = params[0];
final String url = "http://" + serverAdress + val;
try {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
HttpResponse response = client.execute(getRequest);
InputStream inputStream = null;
inputStream = response.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
Log.e("", "parse error2: " + e.toString());
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
/*
----- HERE I NEED IF SOMETHING LIKE arrives ----
if (serverResponse.toString().equals("?a=11&b=22&d=33") ){
Toast.makeText(Test.this, "a=" a + "b=" b + "c=" c , Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Test.this, "Connection is not ok", Toast.LENGTH_SHORT).show();
}
*/
}
@Override
protected void onPreExecute() {
Text_Arduino.setText("pleas wait...");
}
} // private class HttpRequestTask
}//公共类测试结束
推荐答案
您有 2 个选择:
轮询服务器:在服务器上编写一个函数,返回您需要的结果.然后在android中设置一个定时器,每隔几秒调用一次这个函数
Poll the server: write a function on the server that returns the results you need. Then setup a timer in android that calls this function every few seconds
从服务器推送消息:使用推送服务将您的结果发送到 android 应用程序.例如:https://firebase.google.com/docs/cloud-messaging/
Push messages from the server: use a push service to send your results to the android app. ex: https://firebase.google.com/docs/cloud-messaging/
解析您的服务器响应字符串:
To parse your server response string:
String serverResponse = "?a=11&b=22&d=33";
String[] args = serverResponse.substring(1).split("&");
String a = args[0].split("=")[1];
String b = args[1].split("=")[1];
String c = args[2].split("=")[1];
这篇关于Android - 读取 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!