解析数据出错org.json.JSONException: java.lang.Integer类型的值403不能转换为J


Error parsing data org.json.JSONException: Value 403 of type java.lang.Integer cannot be converted to JSONArray

我正在跟随教程学习如何从android应用程序连接到数据库(https://www.youtube.com/watch?v=smVIDycK3-k).I遵循教程中的所有步骤)。但是最后我在运行应用程序时得到这个错误(解析数据org.json.JSONException: java.lang.Integer类型的值403不能转换为JSONArray)MainActivity.java看起来像这样

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fetch= (Button) findViewById(R.id.fetch);
    text = (TextView) findViewById(R.id.text);
    et = (EditText) findViewById(R.id.et);
    fetch.setOnClickListener(this);
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new         ProgressDialog(MainActivity.this);
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
       progressDialog.setMessage("Fetching data...");
       progressDialog.show();
       progressDialog.setOnCancelListener(new OnCancelListener() {
    @Override
        public void onCancel(DialogInterface arg0) {
        task.this.cancel(true);
       }
    });
     }
       @Override
    protected Void doInBackground(String... params) {
      String url_select = "http://www.andyapp.byethost4.com/demo.php";
      HttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url_select);
          ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
        try {
        httpPost.setEntity(new UrlEncodedFormEntity(param));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        //read content
        is =  httpEntity.getContent();                  
        } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());
        //Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show();
        }
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = "";
        while((line=br.readLine())!=null)
        {
           sb.append(line+"'n");
        }
            is.close();
            result=sb.toString();               
                } catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error converting result "+e.toString());
                }
            return null;
        }
    protected void onPostExecute(Void v) {
        // ambil data dari Json database
        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
            JSONObject Jasonobject = null;
            Jasonobject = Jarray.getJSONObject(i);
            //get an output on the screen
            String name = Jasonobject.getString("name");
            String db_detail="";
            if(et.getText().toString().equalsIgnoreCase(name)) {
            db_detail = Jasonobject.getString("detail");
            text.setText(db_detail);
            break;
            }

            }
            this.progressDialog.dismiss();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
    }
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()) {
    case R.id.fetch : new task().execute();break;
    }
}

}

demo.php如下所示

<?php
mysql_connect("sql306.byethost4.com","user_name","******") or      die(mysql_error());
mysql_select_db("db_name");
$sql=mysql_query("select * from demo");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

那么我如何克服这个错误呢?非常感谢你的帮助。

您应该发布从服务器收到的json响应。但是错误消息说您收到了字符串"403",这意味着"禁止"。你不能访问你的php脚本。因此,在onPostExceute中,以下行失败:

JSONArray Jarray = new JSONArray(result);