普通网友 2015-08-25 02:49
浏览 57

Json从android发送到php默认为index.php

I am new to PHP. I have an android app that sends JSON string to myphp.php but it gets redirected to index.php. Is this normal behavior? How can I go around that?

Code

URL url= null;
HttpURLConnection urlConn;
url = new URL ("http://192.***.*.**/myserver/myphp.php");
urlConn = (HttpURLConnection)updateurl.openConnection();

This is the content of myphp.php.

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
        //include '/index.php';

        $json = file_get_contents("php://input");
        $decoded = json_decode($json, TRUE);
        $lat = $decoded['lat'];
        $lon = $decoded['lon'];

        sendID($pasid, $lat, $lon);

        function sendID($tokenid, $lat, $lon) {
//request url
        $url = 'https://android.googleapis.com/gcm/send';

//your api key
        $apiKey = 'key';

//payload data
        $data = array('lat' => $lat, 'lon' => $lon);
        //$registration_ids = array($tokenid);
        $message_Id = "0000001";

        $fields = array('to' => $tokenid, 'data' => $data);

//http header
        $headers = array('Authorization: key=' . $apiKey,
        'Content-Type: application/json');

//curl connection
        $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_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        curl_error($ch); //see the error details
        $result = curl_exec($ch);

        curl_close($ch);

        echo $result;
        }
  • 写回答

1条回答 默认 最新

  • dongzi4030 2015-08-25 04:55
    关注

    I used the below method, but with namevaluepairs and it was working. If your php is configured well, it won't redirect to index file. Can you share the myphp.php file contents?

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.***.*.**/myserver/myphp.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    

    Also try to open myphp.php from your browser.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题