如何将 url 参数发送到 php 服务器并返回对 android 的响应
How to send url paremeters to php server and get back response to android
我正在尝试创建一个基本应用程序,我想在其中将请求从 android 发送到 php 服务器,如下所示:http://www.eg.com/eg.php?a=blabla:blablabla:bla。
然后当我得到这些数据时,我想做这样的事情:
if(isset($_GET['a'])) {
$a = $_GET['a'];
$array = explode(':', $a);
$data1 = $array[0];
if($data1 == "blabla") {
Send response to android here..
}
}
问题是我不知道如何在 android 和 php 服务器之间发送数据。我看了很多答案,但大多数发送 json 数据或使用折旧的 apache http 库,或者没有谈论任何关于 php 服务器端的内容,或者是特定于该人的 secnario。请您就如何执行此操作给我一个非常明确的答案,谢谢:)
如果有一个答案已经涵盖了这个问题,请在投票给我之前向我提供该答案的 url。
我终于在评论和研究时间的帮助下找到了一个简单的解决方案:
您可以使用以下方法简单地调用此方法:
HashMap<String , String> postDataParams = new HashMap<String, String>();
postDataParams.put("name", "value");
performPostCall("URL", postDataParams);
Java代码:
public String performPostCall(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
Log.e("Res:", response);
}
}
else {
response="";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
在 php 端,您可以使用以下方法获取数据:
$_POST['name']
您只需执行以下操作即可发回响应:
echo "response here..";
我正在尝试创建一个基本应用程序,我想在其中将请求从 android 发送到 php 服务器,如下所示:http://www.eg.com/eg.php?a=blabla:blablabla:bla。
然后当我得到这些数据时,我想做这样的事情:
if(isset($_GET['a'])) {
$a = $_GET['a'];
$array = explode(':', $a);
$data1 = $array[0];
if($data1 == "blabla") {
Send response to android here..
}
}
问题是我不知道如何在 android 和 php 服务器之间发送数据。我看了很多答案,但大多数发送 json 数据或使用折旧的 apache http 库,或者没有谈论任何关于 php 服务器端的内容,或者是特定于该人的 secnario。请您就如何执行此操作给我一个非常明确的答案,谢谢:)
如果有一个答案已经涵盖了这个问题,请在投票给我之前向我提供该答案的 url。
我终于在评论和研究时间的帮助下找到了一个简单的解决方案:
您可以使用以下方法简单地调用此方法:
HashMap<String , String> postDataParams = new HashMap<String, String>();
postDataParams.put("name", "value");
performPostCall("URL", postDataParams);
Java代码:
public String performPostCall(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
Log.e("Res:", response);
}
}
else {
response="";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
在 php 端,您可以使用以下方法获取数据:
$_POST['name']
您只需执行以下操作即可发回响应:
echo "response here..";