从android应用程序或java中执行PHP脚本
我尽量避免问这个问题,因为之前也有人问过类似的问题,但是,解决方案仍然不清楚 我目前有一个PHP脚本,它在执行时会向数据库中的所有用户发送通知 此脚本存储在localhost中 以下是脚本:从android应用程序或java中执行PHP脚本,java,php,android,notifications,Java,Php,Android,Notifications,我尽量避免问这个问题,因为之前也有人问过类似的问题,但是,解决方案仍然不清楚 我目前有一个PHP脚本,它在执行时会向数据库中的所有用户发送通知 此脚本存储在localhost中 以下是脚本: <?php function send_notification ($tokens, $message) { $url = 'https://fcm.googleapis.com/fcm/send'; $fields = array( 'registration_i
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = AIzaSyDYzkdd3aPZtgg4VkZQkLu7vombVecin4M',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost","root","root","fcm");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => " Welcome to Remi.");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
有没有一种可能的解决方案可以从Android java应用程序执行PHP脚本?
谢谢您的时间。
我在从android/java启动PHP脚本时遇到了麻烦代码>。对的确我可以看出你在代码上有哪些问题。但是,如果您甚至不知道发生了什么问题和发生了什么……哦,对不起,java代码是我试图从java中查找和执行PHP脚本的尝试。我试过几种方法,但都没用。运行时也没有错误。点击按钮什么也不做,我不确定我的代码中缺少了什么(或者即使这是正确的方法),它会立即使你的应用程序崩溃。您应该在catch块中放置Toast(),否则用户不知道ioexception。但是你的应用程序现在崩溃了。。如果没有,那么你用错了按钮。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class CreateActivity extends AppCompatActivity {
WebView myBrowser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
final TextView mTextView = (TextView) findViewById(R.id.desc);
Button create = (Button) findViewById(R.id.email_sign_in_button);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
launchPHP();
launchPHP2();
} catch (Exception e){
//
}
//myBrowser = (WebView) findViewById(R.id.mybrowser);
//assert myBrowser != null;
//myBrowser.loadUrl("file:///android_asset/reminder.htaccess");
}
});
}
private void launchPHP() throws Exception{
URL url = new URL("http://192.168.0.20/fcm/push_notification.php/");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
private void launchPHP2() throws Exception{
URL url = new URL("http://192.168.0.20/fcm/push_notification.php/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStream is = conn.getInputStream();
} else {
InputStream err = conn.getErrorStream();
}
}
}