json编码在android中获取错误

时间:2016-09-06 17:02:20

标签: php android json

我有一个页面用php回显了一些json:

http://exception-piano.gigfa.com/json.php

我想在android Studio中解码这个,我的应用程序适用于其他json页面,但是当我使用这个特定页面时,我收到此错误:

  

此网站需要使用Javascript工具,请启用Javascript in   您的浏览器或使用支持Javascript的浏览器。

这是我的json编码:

<?php 

$servername = "xxx";
$username = "yyy";
$password = "zzz";
$dbname = "gigfa_18832988_tamrin";

    $con = mysqli_connect($servername,$username,$password,$dbname);      
    $sql = "SELECT * FROM t;";

    $result = mysqli_query($con,$sql);

    $response=array();
    while($row=mysqli_fetch_array($result))
    {
        array_push($response,array("Week"=>$row[0],"Exercise"=>$row[1]));
    }

    echo json_encode(array("server_response"=>$response));        
    mysqli_close($con);        
    ?>

这是我的android代码:

public class MainActivity extends AppCompatActivity {

    String name;
    String id;
    String word;

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

    public  void get (View view){
        DownloadTask task = new DownloadTask();
        task.execute("http://exception-piano.gigfa.com/json.php");
    }

    public class DownloadTask extends AsyncTask<String, Void, String> {
        TextView TEXTVIEW = (TextView) findViewById(R.id.textView);
        TextView TEXTVIEW2 = (TextView) findViewById(R.id.textView2);
        //TextView TEXTVIEW3 = (TextView) findViewById(R.id.textView3);
        //ListView LISTVIEW = (ListView) findViewById(R.id.listView);

        @Override
        protected String doInBackground(String... urls) {

            String result = "";
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);    
                urlConnection = (HttpURLConnection) url.openConnection();

                InputStream in = urlConnection.getInputStream();    
                InputStreamReader reader = new InputStreamReader(in);

                int data = reader.read();

                while (data != -1) {    
                    char current = (char) data;    
                    result += current;    
                    data = reader.read();    
                }

                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {   
                JSONObject note = new JSONObject(result);
                String noteString = note.getString("server_response");
                JSONArray arr = new JSONArray(noteString);

                //for loop for puting on jsonObject

                for (int i = 0; i < arr.length(); i++) {

                    JSONObject jsonPart = arr.getJSONObject(i);

                    //Put on variables
                    Week = jsonPart.getString("Week");
                    Exercise = jsonPart.getString("Exercise");   

                    //Print on TextView
                    TEXTVIEW.setText("Week : " + Week);
                    TEXTVIEW2.setText("Exercise : " + Exercise);    
                }   

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

3 个答案:

答案 0 :(得分:0)

它可能是ob字符,在服务器输出json之前尝试清理它:

ob_clean();
retur/echo....json

答案 1 :(得分:0)

试试这个

            using (var connection = new SqlConnection(Properties.Settings.Default.connectionString))
            {
                connection.Open();
                using (var fs = File.Open(Properties.Settings.Default.filePath, FileMode.Open))
                {
                    var sql = "INSERT INTO TEST (Stream) VALUES (@fs)";

                    var dParams = new DynamicParameters();
                    dParams.Add("@fs", fs, DbType.Binary);

                    connection.Execute(sql, dParams);
                }
            }

答案 2 :(得分:0)

好的,我发现问题出在我的主机上。它是免费的主机,显然,他们在政策中不允许这样做。我改变了主人,一切都还好。