解析数据时出错org.json.JSONException:java.lang.Integer类型的值403无法转换为JSONArray

时间:2015-07-22 13:39:28

标签: java php android json

我正在按照教程学习从Android应用程序连接数据库(https://www.youtube.com/watch?v=smVIDycK3-k).I遵循教程中的所有步骤。但最后我在运行应用程序时遇到此错误(解析数据时出错) .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();
?>

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

1 个答案:

答案 0 :(得分:0)

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

JSONArray Jarray = new JSONArray(result);