本地主机中的php文件未获取从Android应用程序发送的json数组

时间:2014-06-30 19:21:11

标签: php android json

我有一个应用程序可以生成这个json数组:[{" Name":" Apple"," Quantity":" 8" }, {"名称":"牛肉""数量":" 9"},{"名称":&# 34; Vegitable""数量":" 5"}]

我正在使用json解析器类将此数组发送到本地主机中的php文件。该应用程序成功地将数据发布到php文件,但作为回报获得此垃圾值。 logcat视图是

06-30 15:08:32.381: W/System.err(1157):     at java.lang.Thread.run(Thread.java:841)
06-30 15:08:32.381: W/System.err(1157): [ 06-30 15:08:32.381  1157: 1172 D/    <br />
06-30 15:08:32.381: W/System.err(1157): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspa    <br />
06-30 15:08:32.381: W/System.err(1157): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: json in C:\wamp\www\bootstrap-dist\postingdata.php on line <i>33</i></th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0090</td><td bgcolor='#eeeeec' align='right'>146968</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\bootstrap-dist\postingdata.php' bgcolor='#eeeeec'>..\postingdata.php<b>:</b>0</td></tr>
06-30 15:08:32.381: W/System.err(1157): </table></font>
06-30 15:08:32.381: W/System.err(1157): <br />
06-30 15:08:32.381: W/System.err(1157): <font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Invalid argument supplied for foreach() in C:\wamp\www\bootstrap-dist\postingdata.php on line <i>35</i></th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
06-30 15:08:32.381: W/System.err(1157): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0090</td><td bgcolor='#eeeeec' align='right'>146968</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\bootstrap-dist\postingdata.php' bgcolor='#eeeeec'>..\postingdata.php<b>:</b>0</td></tr>
06-30 15:08:32.381: W/System.err(1157): </table></font>

我无法弄清楚问题所在,以及如何从我的本地主机中的php文件接收应用程序生成的json数组。所以请有人帮我解决这个问题。

这是我的JsonParse类:

 public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getandpostJSONFromUrl(String url, String method,JSONArray name) {

        // Making HTTP request
        try {
            // defaultHttpClient
            if (method.equals("POST") ) {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                Log.d("Json Array content",""+name);
                StringEntity se=new StringEntity(name.toString());
                Log.e("Json Array content",""+se);
                se.setContentType("application/json;charset=UTF-8");
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));

                httpPost.setEntity(se);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                httpPost.setHeader("json",json.toString());
//              HttpEntity httpEntity = httpResponse.getEntity();
                int response = httpResponse.getStatusLine().getStatusCode();
//              is = httpEntity.getContent();
//              Log.e("posting Status", "Post status: " + response);
                 String text = null;
                 if(response==200){

                        HttpEntity entity=httpResponse.getEntity();
                        text=EntityUtils.toString(entity);

                        try {
                            JSONArray jsonArray = new JSONArray(text);
                            //text=jsonArray.getJSONObject(0).getJSONArray(name);
                            text=jsonArray.getString(0);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        String tag=text;
                        Log.d(tag, text);
                    }


            }
            if (method.equals("GET") ) {

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }

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

        if (method == "POST") {
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is));
            } catch (Exception e) {
                Log.e("Buffer error", "Buffer error" + e);
            }

        } else if (method == "GET") {

            try {

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();

            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }

            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
        }

        // return JSON String
        return jObj;

    }

}

这是我用来获取json数组的php文件:

 <?php

    define('DB_NAME', 'a1422982_sshop');
                    define('DB_USER', 'root');
                    define('DB_PASSWORD', '');
                    define('DB_HOST', 'localhost');


                    $link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

                    if(!$link){
                    die('could not connect: '.msql_error());
                    }

                    $db_selected=mysql_select_db(DB_NAME, $link);

                    if(!$db_selected){
                    die('Can not  use '.DB_NAME.':'.mysql_error());
                    }              



                    $jarray = $_POST['json'];
                    $parsed = json_decode($jarray , true);
                    foreach ($parsed as $obj) {
                         $ProductName = $obj['Name'];   
                         $ProductQuantity= $obj['Quantity'];

                         echo   $ProductName." ".$ProductQuantity;
                     }




    ?>

0 个答案:

没有答案