duanla1996 2019-07-12 14:33
浏览 84

试图从android发布JSON数组

I used simple Namevaluepairs in android to post some of my data into a localhost php-server i made before. In the meantime, i needed to post those datas in json objects and the new code was:

public class scanner extends AppCompatActivity {
    private static final String TAG = "scanner log";

    private Button buttonScan;
    private TextView tv1, tv2;
    private IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);

        new postDataAsync().execute();

        buttonScan = (Button) findViewById(R.id.buttonScan);
        tv1 = (TextView) findViewById(R.id.tvprice);
        tv2 = (TextView) findViewById(R.id.tvdes);

        qrScan = new IntentIntegrator(this);

        buttonScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                qrScan.initiateScan();
            }
        });
    }

    public class postDataAsync extends AsyncTask<String, String, String> {
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                postData();
            } catch (NullPointerException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String lenghtOfFile) {
        }
    }

    private void postData() {
        try {
            String postReceiverUrl = "http://192.168.1.107/bot/post.php";
            Log.v(TAG, "postURL: " + postReceiverUrl);

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(postReceiverUrl);

            List<NameValuePair> nameValuePairs = new ArrayList();

            DatabaseHandler db = new DatabaseHandler(this);
            db.openDB();
            Cursor c = db.getAllDatas();

            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();

            while (c.moveToNext()) {
                String name = c.getString(1);
                String price = c.getString(2);
                //nameValuePairs.add(new BasicNameValuePair("name", name));
                //nameValuePairs.add(new BasicNameValuePair("price", price));

                jsonObject.put("name", name);
                jsonObject.put("price", price);

                jsonArray.put(jsonObject);

                //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                //HttpResponse response = httpClient.execute(httpPost);
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                try {
                    JSONObject obj = new JSONObject(result.getContents());
                    tv1.setText(obj.getString("name"));
                    tv2.setText(obj.getString("price"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    /*When Control Comes Over, Means You Can Show Anything Up there!*/
                    Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

And in my localhost server i have had a php code. I can finish the code(posting json), and my php code was:

<?php

$name=$_POST["name"]; 
$price = $_POST["price"];

$result = '<br>' . $name . ' ' . $price;

$filename="submitted-msg.html";
file_put_contents($filename,$result,FILE_APPEND);

$androidmessages=file_get_contents($filename);

echo $androidmessages;
?>

After converting the values into json object and putting them in json array, i just wanna know how could i receive these values from the php-server, as i don't have $_POST["name"] or whatever now! By which variable in php i should use so that i could post that json array. Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongzhanlian6289 2019-07-12 14:44
    关注

    Try this

    $postdata = file_get_contents("php://input");
    $jsondata = json_decode($postdata);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧