从Android的JSON到PHP输入 [英] json from Android to php input

查看:106
本文介绍了从Android的JSON到PHP输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android发布数据作为JSON PHP脚本,但它似乎只是没有读我送的数据。

PHP

 标题(内容类型:应用程序/ JSON');
$ JSON =的file_get_contents('PHP://输入');
的print_r($ JSON);

数据

<$p$p><$c$c>{\"Date\":\"2012-05-31\",\"Device_ID\":\"soaptester\",\"Time\":\"15:22:59\",\"Longitude\":0.33,\"Latitude\":51.01}

Java的

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    SP = getShared preferences(SoapTester,Context.MODE_PRIVATE);
    的setContentView(R.layout.main);
    字符串的地址= sp.getString(地址,NULL);
    如果(地址== NULL){
        。sp.edit()putString(地址,http://192.168.0.8/lt_data/test.php).commit();
    }    addressTextView =(EditText上)findViewById(R.id.address);
    addressTextView.setOnEditorActionListener(新TextView.OnEditorActionListener(){        @覆盖
        公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
            。sp.edit()putString(地址,将String.valueOf(v.getText()))提交()。
            返回true;
        }
    });    轨道=新LocationTrack();
    track.setDate(2012-05-31);
    track.setTime(15点22分59秒);
    track.setLatitude(51.01);
    track.setLongitude(0.33);
    track.setDevice_ID(soaptester);    输出=新GSON()的toJSON(轨道)。    outputTextView =(的TextView)findViewById(R.id.output);
    outputTextView.setText(输出);}@覆盖
保护无效onResume(){
    super.onResume();
    addressTextView.setText(sp.getString(地址,));
}公共无效sendPost(查看视图){    ServiceResponse R = Resting.post(sp.getString(地址,),80,输出EncodingTypes.UTF8);
    ((的TextView)findViewById(R.id.response))的setText(将String.valueOf(R))。}

在测试负载我似乎无法找出为什么它没有从Android应用程序接收数据。


解决方案

也许尝试使用一些工具,如netcat来看看你正确与否发送您的JSON消息。或尝试使用网络流量一些工具(Wireshark的,tcpdump的)。

I am trying to post data as json from Android to a PHP script, however it just doesn't seem to read the data I am sending.

PHP

header('Content-type: application/json');
$json = file_get_contents('php://input');
print_r($json);

Data

{"Date":"2012-05-31","Device_ID":"soaptester","Time":"15:22:59","Longitude":0.33,"Latitude":51.01}

Java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sp = getSharedPreferences("SoapTester", Context.MODE_PRIVATE);
    setContentView(R.layout.main);
    String address = sp.getString("address", null);
    if (address == null) {
        sp.edit().putString("address", "http://192.168.0.8/lt_data/test.php").commit();
    }

    addressTextView = (EditText) findViewById(R.id.address);
    addressTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            sp.edit().putString("address", String.valueOf(v.getText())).commit();
            return true;
        }
    });

    track = new LocationTrack();
    track.setDate("2012-05-31");
    track.setTime("15:22:59");
    track.setLatitude(51.01);
    track.setLongitude(0.33);
    track.setDevice_ID("soaptester");

    output = new Gson().toJson(track);

    outputTextView = (TextView) findViewById(R.id.output);
    outputTextView.setText(output);

}

@Override
protected void onResume() {
    super.onResume();
    addressTextView.setText(sp.getString("address", ""));
}

public void sendPost(View view) {

    ServiceResponse r = Resting.post(sp.getString("address", ""), 80, output, EncodingTypes.UTF8);
    ((TextView)findViewById(R.id.response)).setText(String.valueOf(r));

}

After loads of testing i just can't seem to find out why it is not receiving the data from the android application.

解决方案

Maybe try to use some tools like netcat to see if you sending your json message correctly or not. Or try to use some tools for network traffic (wireshark, tcpdump).

这篇关于从Android的JSON到PHP输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆