Java 在000webhost服务器上上载时,For each循环不执行

Java 在000webhost服务器上上载时,For each循环不执行,java,php,android,json,Java,Php,Android,Json,我有一个将数据发布到在线服务器中的php文件的应用程序。当文章写完后,我得到了一堆垃圾html代码。其中说我有一个php错误,这是第33行为每个错误提供的无效参数。但是,如果在localhost中运行,则不会出现此问题。我不明白为什么会发生这个问题。所以请有人帮我解决这个问题 下面是我的jsonparser类 public class JSONParser { static InputStream is = null; static JSONObject jObj = null

我有一个将数据发布到在线服务器中的php文件的应用程序。当文章写完后,我得到了一堆垃圾html代码。其中说我有一个php错误,这是第33行为每个错误提供的无效参数。但是,如果在localhost中运行,则不会出现此问题。我不明白为什么会发生这个问题。所以请有人帮我解决这个问题

下面是我的jsonparser类

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 == "POST") {


                HttpParams params = new BasicHttpParams();
                //params.setParameter("data", auth);
                HttpClient httpclient = new DefaultHttpClient(params);

                HttpPost httpPost = new HttpPost(url);


                List<NameValuePair> postParams = new ArrayList<NameValuePair>();
                postParams.add(new BasicNameValuePair("json", name.toString()));

                for (NameValuePair nvp : postParams) {
                    String name2 = nvp.getName();
                    String value = nvp.getValue();
                    Log.d("NameValue pair content", ""+name2+""+value);
                }


                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams,HTTP.UTF_8);


                httpPost.setEntity(entity);
                HttpResponse response = httpclient.execute(httpPost);
                String responseBody = EntityUtils.toString(response.getEntity()); 
                Log.d("",responseBody);
            }
            if (method == "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;

    }

}
下面是服务器上的php文件

<?php
        header('Content-type: application/json');
        /*define('DB_NAME', 'a1422982_sshop');
                        define('DB_USER', 'root');
                        define('DB_PASSWORD', '');
                        define('DB_HOST', 'localhost');*/

        define('DB_NAME', 'onlineshop');
        define('DB_USER', 'shop');
        define('DB_PASSWORD', 'pass');
        define('DB_HOST', 'mysql28.000webhost.com'); 


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

                        //var_dump(json_decode ($_POST['json'])));

                        if($_POST['json']){
                            $parsed = json_decode($_POST['json'],TRUE);

                            $i=0;

                            foreach ($parsed as $obj) {
                                 $ProductName = $obj['Name'];   
                                 $ProductQuantity= $obj['Quantity'];
                                 $sql="Update productlist Set Quantity='$ProductQuantity' where Name='$ProductName';";

                                 $retval = mysql_query( $sql, $link );
                                  if(! $retval )
                                    {
                                      die('Could not get data: ' . mysql_error());
                                    }
                                 $i++;

                                 echo   $ProductName." ".$ProductQuantity;
                             }
                        }else{
                            echo "empty";
                        }


?>

HttpPost请求中缺少选项,请将实体元数据和结果实体设置为字符串

在java代码中,可以执行以下操作:

   Map<String, String> postData = new HashMap<String, String>();
   postData.put("KEY", "yourvalue");
   JSONObject holder = new JSONObject(postData);

   StringEntity jsonStringEntity = new StringEntity(holder.toString());

   httpost.setEntity(jsonStringEntity);
   httpost.setHeader("Accept", "application/json");
   httpost.setHeader("Content-type", "application/json");

这样,您的PHP代码实际上可以解析post数据,因为json解码期望json作为参数。

您应该添加error\u reportingE\u ALL;在PHP文件的开头,您可能会看到一些错误,不要使用搜索第33行,而是告诉它是哪一行。if$_POST['json']{最好改为if isset$_POST['json']{。在第33行->foreach$解析为$obj{@greenappsThen$parsed无效。请找出原因。打印或让脚本返回json文本,看看它是否与您发布的文本不同。并使用isset!告诉您是否应用了所有建议的更改。