dongshu4221
dongshu4221
2017-01-27 12:51
浏览 46

无法在Android中使用PHP脚本插入MySQL

I'm trying to insert the firebase token_key into the mysql using php script by Volley library but can't able to implement it.I'm facing "AuthFailureError" exception.

I know i'm doing some mistake but can't able to figure it out.I'm attaching the code snipet down here,Please give me some suggestions:

init.php

<?php

$host = "localhost";
$db_user = "root";
$db_password = "*****";
$db_name = "fcmdb";

$con = mysqli_connect($host,$db_user,$db_password,$db_name);
if($con)
echo "Connection Success...";
else
echo "Connection Failure...";

?>

fcm_insert.php

<?php

require "init.php";
$fcm_token ='';
$fcm_token = $_POST["key_token"];
$sql = "insert into fcm_info values('".$fcm_token."');";
mysqli_query($con,$sql);
mysqli_close($con);

?>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private Button btnClick;
String app_server_url="http://192.168.0.106:8079/fcmtest/fcm_insert.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnClick=(Button)findViewById(R.id.button);

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sharedPreferences=getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
                final String token=sharedPreferences.getString(getString(R.string.FCM_TOKEN),"");

                StringRequest stringRequest=new StringRequest(Request.Method.POST, app_server_url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("Error",response.toString());
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i("Error",error.toString());
                    }
                })

                {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params=new HashMap<String, String>();
                        params.put("key_token",token);

                        return params;
                    }
                };
                MySingleton.getMySingleton(MainActivity.this).addToRequestQueue(stringRequest);
            }
        });
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtng25909
    dtng25909 2017-01-27 13:04
    已采纳

    Hey hi please follow below link

    It will be help you

    This link for php with android

    点赞 评论

相关推荐