尝试从Android发布JSON数组

时间:2019-07-12 14:33:42

标签: php android json

我在Android中使用了简单的Namevaluepairs,将我的一些数据发布到了我之前创建的localhost php服务器中。同时,我需要将这些数据发布到json对象中,新代码为:

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);
        }
    }
}

在我的本地主机服务器上,我有一个php代码。我可以完成代码(发布json),而我的php代码是:

<?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;
?>

将值转换为json对象并将它们放入json数组后,我只想知道如何从php服务器接收这些值,因为我现在没有$_POST["name"]或其他任何东西!我应该使用php中的哪个变量,以便可以发布该json数组。 预先感谢!

1 个答案:

答案 0 :(得分:0)

尝试一下

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