几周前我刚刚开始进行Android编程,并负责编写一个程序,向外部WeMos D1 mini pro发送HTTP GET请求,以便上传到内部SQL数据库。
这是我的主要活动代码
@InitBinder
这是我的服务java文件
package com.sp.androidesp8266wifi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
public class MainActivity extends Activity implements View.OnClickListener {
public final static String PREF_IP = "PREF_IP_ADDRESS";
public final static String PREF_PORT = "PREF_PORT_NUMBER";
// declare buttons and text inputs
private ToggleButton buttonPin2,buttonPin3,buttonPin4,buttonPin5,buttonPin6,buttonPin7;
// shared preferences objects used to save the IP address and port so that the user doesn't have to
// type them next time he/she opens the app.
private EditText editTextIPAddress, editTextPortNumber;
SharedPreferences.Editor editor;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("HTTP_HELPER_PREFS",Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
// assign buttons
buttonPin2 = (ToggleButton)findViewById(R.id.buttonPin2);
buttonPin3 = (ToggleButton)findViewById(R.id.buttonPin3);
buttonPin4 = (ToggleButton)findViewById(R.id.buttonPin4);
buttonPin5 = (ToggleButton)findViewById(R.id.buttonPin5);
buttonPin6 = (ToggleButton)findViewById(R.id.buttonPin6);
buttonPin7 = (ToggleButton)findViewById(R.id.buttonPin7);
// set button listener (this class)
buttonPin2.setOnClickListener(this);
buttonPin3.setOnClickListener(this);
buttonPin4.setOnClickListener(this);
buttonPin5.setOnClickListener(this);
buttonPin6.setOnClickListener(this);
buttonPin7.setOnClickListener(this);
// get the IP address and port number from the last time the user used the app,
// put an empty string "" is this is the first time.
editTextIPAddress = (EditText)findViewById(R.id.editTextIPAddress);
editTextPortNumber = (EditText)findViewById(R.id.editTextPortNumber);
editTextIPAddress.setText(sharedPreferences.getString(PREF_IP,""));
editTextPortNumber.setText(sharedPreferences.getString(PREF_PORT,""));
}
@Override
public void onClick(View view) {
// get the ip address
String ipAddress = editTextIPAddress.getText().toString().trim();
// get the port number
String portNumber = editTextPortNumber.getText().toString().trim();
// save the IP address and port for the next time the app is used
editor.putString(PREF_IP,ipAddress); // set the ip address value to save
editor.putString(PREF_PORT,portNumber); // set the port number to save
editor.commit(); // save the IP and PORT
// get the pin number
String parameterValue = "";
String parameter = "";
// get the pin number from the button that was clicked
if(view.getId()==buttonPin2.getId())
{
if (buttonPin2.getText().equals("ON"))
{
parameter = "2";
parameterValue = "1";
}
}
else if(view.getId()==buttonPin3.getId())
{
if (buttonPin3.getText().equals("ON"))
{
parameter = "3";
parameterValue = "1";
}
}
else if(view.getId()==buttonPin4.getId())
{
if (buttonPin4.getText().equals("ON"))
{
parameter = "4";
parameterValue = "1";
}
}
else if(view.getId()==buttonPin5.getId())
{
if (buttonPin5.getText().equals("ON"))
{
parameter = "5";
parameterValue = "1";
}
}
else if(view.getId()==buttonPin6.getId())
{
if (buttonPin6.getText().equals("ON"))
{
parameter = "6";
parameterValue = "1";
}
}
else if(view.getId()==buttonPin7.getId())
{
if (buttonPin7.getText().equals("ON"))
{
parameter = "7";
parameterValue = "1";
}
}
Intent intent = new Intent("com.sp.androidesp8266wifi.ServiceStatusUpdate");
intent.putExtra("parameterValue", parameterValue);
intent.putExtra("ipAddress", ipAddress);
intent.putExtra("portNumber", portNumber);
intent.putExtra("parameter", parameter);
// execute HTTP request
if(ipAddress.length()>0 && portNumber.length()>0) {
startService(intent);
}
}
/**
* Description: Send an HTTP Get request to a specified ip address and port.
* Also send a parameter "parameterName" with the value of "parameterValue".
* @param parameterValue the pin number to toggle
* @param ipAddress the ip address to send the request to
* @param portNumber the port number of the ip address
* @param parameterName
* @return The ip address' reply text, or an ERROR message is it fails to receive one
*/
/* public String sendRequest(String parameterValue, String ipAddress, String portNumber, String parameterName) {
String serverResponse = "ERROR";
try {
HttpClient httpclient = new DefaultHttpClient(); // create an HTTP client
// define the URL e.g. http://myIpaddress:myport/?pin=13 (to toggle pin 13 for example)
URI website = new URI("http://"+ipAddress+":"+portNumber+"/"+parameterName+"/"+parameterValue);
HttpGet getRequest = new HttpGet(); // create an HTTP GET object
getRequest.setURI(website); // set the URL of the GET request
HttpResponse response = httpclient.execute(getRequest); // execute the request
} catch (ClientProtocolException e) {
// HTTP error
serverResponse = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
// IO error
serverResponse = e.getMessage();
e.printStackTrace();
} catch (URISyntaxException e) {
// URL syntax error
serverResponse = e.getMessage();
e.printStackTrace();
}
// return the server's reply/response text
return serverResponse;
} */
/**
* An AsyncTask is needed to execute HTTP requests in the background so that they do not
* block the user interface.
*/
/* private class HttpRequestAsyncTask extends AsyncTask<Void, Void, Void> {
// declare variables needed
private String ipAddress, portNumber;
private Context context;
private String parameter;
private String parameterValue;
/**
* Description: The asyncTask class constructor. Assigns the values used in its other methods.
* @param context the application context, needed to create the dialog
* @param parameterValue the pin number to toggle
* @param ipAddress the ip address to send the request to
* @param portNumber the port number of the ip address
*/
/*public HttpRequestAsyncTask(Context context, String parameterValue, String ipAddress, String portNumber, String parameter)
{
this.context = context;
this.ipAddress = ipAddress;
this.parameterValue = parameterValue;
this.portNumber = portNumber;
this.parameter = parameter;
} */
/**
* Name: doInBackground
* Description: Sends the request to the ip address
* @param voids
* @return
*/
/*@Override
protected Void doInBackground(Void... voids) {
new Handler().postDelayed(new Runnable() {
public void run() {
sendRequest(parameterValue,ipAddress,portNumber, parameter);
}
}, 4000);
return null;
}
/**
* Name: onPostExecute
* Description: This function is executed after the HTTP request returns from the ip address.
* The function sets the dialog's message with the reply text from the server and display the dialog
* if it's not displayed already (in case it was closed by accident);
* @param aVoid void parameter
*/
/**
* Name: onPreExecute
* Description: This function is executed before the HTTP request is sent to ip address.
* The function will set the dialog's message and display the dialog.
*/
}
和我的清单
package com.sp.androidesp8266wifi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class ServiceStatusUpdate extends Service {
String ipAddress, parameter,parameterValue,portNumber;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("com.sp.androidesp8266wifi.ServiceStatusUpdate")) {
ipAddress = intent.getStringExtra("ipAddress");
parameter = intent.getStringExtra("parameter");
parameterValue = intent.getStringExtra("parameterValue");
portNumber = intent.getStringExtra("portNumber");
while (true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
new DoBackgroundTask().execute();
e.printStackTrace();
}
}
}
return START_STICKY;
}
public class DoBackgroundTask extends AsyncTask<Void, Void, Void> {
public void sendRequest(String parameterValue, String ipAddress, String portNumber, String parameterName) {
try {
HttpClient httpclient = new DefaultHttpClient(); // create an HTTP client
// define the URL e.g. http://myIpaddress:myport/?pin=13 (to toggle pin 13 for example)
URI website = new URI("http://"+ipAddress+":"+portNumber+"/"+parameterName+"/"+parameterValue);
HttpGet getRequest = new HttpGet(); // create an HTTP GET object
getRequest.setURI(website); // set the URL of the GET request
HttpResponse response = httpclient.execute(getRequest); // execute the request
} catch (ClientProtocolException e) {
// HTTP error
e.printStackTrace();
} catch (IOException e) {
// IO error
e.printStackTrace();
} catch (URISyntaxException e) {
// URL syntax error
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... params) {
sendRequest(parameterValue,ipAddress,portNumber,parameter);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
new Handler().postDelayed(new Runnable() {
public void run() {
sendRequest(parameterValue,ipAddress,portNumber, parameter);
}
}, 5000);
}
}
}
我刚刚开始编程作为一项任务,并且不知道Android上可用的功能。因此,我做了一些研究并推出了这个程序,但每次按下按钮都会崩溃。
该程序的目标是在单击切换按钮后连续向WeMos发送带有相应参数的HTTP GET请求。我想知道Android是否可以支持同时打开多个切换按钮并同时发送请求。请帮助我,引导我开始我的旅程。谢谢
答案 0 :(得分:0)
,只要单击按钮,就会启动服务
public void onClick(View view) {
[...]
Intent intent = newIntent("com.sp.androidesp8266wifi.ServiceStatusUpdate");
intent.putExtra("parameterValue", parameterValue);
intent.putExtra("ipAddress", ipAddress);
intent.putExtra("portNumber", portNumber);
intent.putExtra("parameter", parameter);
// execute HTTP request
if(ipAddress.length()>0 && portNumber.length()>0) {
startService(intent);
}
然后一个服务实例正在运行并且不会停止。一个不那么好的可能性是在服务中添加this.stopSelf();
,以便每当启动服务实例时自行停止(http://www.stackoverflow.com/questions/1886874/how-to-stop-service-by-itself)
另一种解决方案是将消息(事件)从活动发送到包含http请求信息的后台服务。
在这种情况下,您不会在按钮单击时启动后台服务。您在主活动中启动一次服务,然后在单击按钮时向服务发送消息
示例位于Example: Communication between Activity and Service using Messaging和https://blog.heckel.xyz/2012/06/10/android-example-communication-between-activity-and-service-using-messaging/