我设法建立了一个JSON数组,并且有一个onClick事件,我通过POST将这个JSONArray发送到将处理该数组的php文件中。

现在我不知道数组是否到达目的地,或者PHP是否正确解释了数组。
我的POST呼叫(Android)的代码是:

Button bsave = (Button) findViewById(R.id.button3);
    View.OnClickListener eventHandlerx = new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            JSONObject j1;
            JSONArray jsonarray=new JSONArray();
            ListView lst = (ListView) findViewById(R.id.mylistview);
            int count = lst.getCount();
            for (int i = 0; i < count; i++)
            {
                ViewGroup row = (ViewGroup) lst.getChildAt(i);
                TextView tvId = (TextView) row.findViewById(R.id.fID);
                TextView tvNume = (TextView) row.findViewById(R.id.fTitlu);
                TextView tvUM = (TextView) row.findViewById(R.id.fUM);
                TextView tvPU = (TextView) row.findViewById(R.id.fPU);
                TextView tvCant = (TextView) row.findViewById(R.id.fCant);

                jsonarray.put("Item");
                j1 = new JSONObject();
                try {
                    j1.put("idx", newid);
                    j1.put("id", tvId.getText());
                    j1.put("nume", tvNume.getText());
                    j1.put("um", tvUM.getText());
                    j1.put("pu", tvPU.getText());
                    j1.put("cant", tvCant.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                jsonarray.put(j1);
            }
            Log.d("JSON array","Array to send:"+jsonarray.toString());
            sendJson( urlx, jsonarray);
        }

        private void sendJson(final String urlx, final JSONArray jsonarray) {
                Thread t = new Thread() {
                    public void run() {
                        Looper.prepare(); //For Preparing Message Pool for the child Thread
                        HttpClient client = new DefaultHttpClient();
                        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                        HttpResponse response;
                        try {
                            HttpPost post = new HttpPost(urlx);
                            StringEntity se = new StringEntity( jsonarray.toString());
                            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                            post.setEntity(se);
                            response = client.execute(post);

                            /*Checking response */
                            if(response!=null){
                                HttpEntity entity = response.getEntity();
                                if (entity != null) {
                                    Log.e("POST response",EntityUtils.toString(entity));
                                }else{
                                    Log.e("POST response","NULL "+EntityUtils.toString(entity));
                                }
                            }else{
                                Log.e("POST response","NULL");
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                            Log.e("postJSON","Error at POST with json:"+e.toString());
                        }
                        Looper.loop(); //Loop in the message queue
                    }
                };
                t.start();
            }
    };
    bsave.setOnClickListener(eventHandlerx);


如何检查数组是否已成功发布到php?我认为我可以在php文件中执行此操作...但是如何查看结果?
无论我做什么,日志都会显示:

org.apache.http.conn.EofSensorInputStream@somehexa


所以我被困住了。请给我一些解决方案。
为了返回POSTED值,php文件应该是什么样子,而为了解释结果,我的Android代码应该是什么样子(只需将其显示在类似Log的位置即可进行调试)

我使用的PHP是:

<?php
ob_start();
var_dump($_POST);
die();
$json = file_get_contents('php://input');
$obj = json_decode($json);
print $obj;
result = $obj;

$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.txt","w");
fwrite($fp,$page);
fclose($fp);
?>


但是,该文件未显示...
Eclipse在Log中没有给我任何东西...

谢谢

最佳答案

如果这仅是为了测试数据,最简单的方法是在要提交表单的PHP文件中执行var_dump($_POST);并将响应记录到Web服务器上的某个文件或在Android应用程序中进行控制台。如果要在测试时中断其余代码的执行,可以在die();之后放置var_dump

编辑:

由于您使用的是php://input流,并且已经有一些代码,您可以只使用var_dump($json)或仅echo即可将其备份到您的应用程序中,而不是我之前建议的。这将返回您发布的原始数据,但是您需要在应用中使用某种代码来显示HttpResponse。您可以尝试如下操作:

HttpEntity entity = response.getEntity();
String phpResponse = EntityUtils.toString(entity);


然后记录phpResponse变量。

另外,由于未执行die();之后的所有操作,因此不会生成文件,您可以将其删除并再次检查文件是否存在。

08-28 22:40
查看更多