所以,我试图通过Android中的Post向PHP发送消息 这是Android Java功能:
//enviando para o backend
private void SendtoPHP(String reg) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://bubbledev.com.br/gcm/getdevice.php");
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("regid", "" + reg ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
PHP:
<?php
include 'conecta.php';
$device_token = urldecode($_POST['regid']);
$sql = "INSERT INTO corposa.deviceandroid"
. " (id,device_token)"
. " VALUES (NULL,'$device_token')";
mysqli_query($con,$sql);
&GT;
PHP很好,我已经用另一个Post测试了它并且工作了,但是当Android函数尝试$ device_token时,不要重复任何值,并且SQL保存了一个&#34;&#34;在桌子上的id
答案 0 :(得分:3)
除了ResponseHandler之外,我使用的代码类似于你的代码。 它对我有用。
HttpClient Client = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", sID));
nameValuePairs.add(new BasicNameValuePair("etc", sETC));
try {
String SetServerString = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://your-url.com/script.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = httpclient.execute(httppost, responseHandler);
} catch(Exception ex) {
// failed
}
答案 1 :(得分:1)
我确定有更好的方法可以做到这一点,但这就是我通常处理来自Android的帖子请求的方式:
<?php
{
$input = json_encode(file_get_contents("php://input"));
$deviceToken = $input->regId;
mysqli_query($connection, "INSERT INTO corposa.deviceandroid (`id`, `device_token`) VALUES(NULL, '" . mysqli_real_escape_string($deviceToken) . "')";
}
此外,由于您使用的是POST而不是GET,因此传递给服务器的数据不会被编码,因此此处不需要urldecode。
答案 2 :(得分:0)
代码似乎没问题,只有&#34;奇怪&#34;事情是:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
因为您只在列表中添加一个BasicNameValuePair。也许这就是问题......?
答案 3 :(得分:0)
我在当前项目中使用此代码,希望它也适合您
Map<String, String> kvPairs = new HashMap<String, String>();
kvPairs.put("regid", reg);
HttpClient httpclient = this.getNewHttpClient();
HttpPost httppost = new HttpPost("http://bubbledev.com.br/gcm/getdevice.php");
if (kvPairs != null && kvPairs.isEmpty() == false) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
String k, v;
Iterator<String> itKeys = kvPairs.keySet().iterator();
while (itKeys.hasNext()) {
k = itKeys.next();
v = kvPairs.get(k);
nameValuePairs.add(new BasicNameValuePair(k, v));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
}
HttpResponse response;
response = httpclient.execute(httppost);
String responseString = EntityUtils.toString(response.getEntity());
在你的班级中也需要这个方法
public static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new RecorridoSSL(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
希望我没有忘记任何事情,其他方式告诉我
答案 4 :(得分:0)
Guys错误发生在我的PHP中
$device_token = urldecode($_POST['regid']);
这个urldecode弄乱了我的代码哈哈
感谢所有帮助