有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在安卓应用程序上解析http post的json结果时出现java错误

我有一个返回这个json数组的php脚本

{"PID":"1","PName":"Guitar","Brand":"Fender","Price":"110","Cat#":"1","Typ#":"1"}

我正在制作一个简单的应用程序,将这些结果放入几个文本视图中。如上所述,每次仅返回一个产品

当我运行应用程序时,我得到了以下错误:org.json.JSONException: Value

{"Typ#":"1","Brand":"test","Cat#":"1","PName":"Test","PID":"2","Price":"120"}

无法将类型为org.json.JSONObject的转换为JSONArray

这是我的密码。json结果或代码是否有问题

public class MainActivity extends ActionBarActivity {

TextView tvname;
TextView tvbrand;

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

    tvname = (TextView) findViewById(R.id.tvName);
    tvbrand = (TextView) findViewById(R.id.tvBrand);
    Button btnPost = (Button) findViewById(R.id.btnPost);

    btnPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new getPro().execute();
        }
    });
}//end of on create


private class getPro extends AsyncTask<String,String,Void>{

    private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    InputStream inputStream = null;
    String result = "";

    protected void onPreExecute() {
        progressDialog.setMessage("Downloading your data...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                getPro.this.cancel(true);
            }
        });
    }


    @Override
    protected Void doInBackground(String... strings) {

        String url_select = "http://10.0.2.2/OnetoOne/getProduct.php";

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("pid", "2"));

        try {
            // Set up HTTP post

            // HttpClient is more then less deprecated. Need to change to URLConnection
            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(url_select);
            httpPost.setEntity(new UrlEncodedFormEntity(param));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // Read content & Log
            inputStream = httpEntity.getContent();
        } catch (UnsupportedEncodingException e1) {
            Log.e("UnsupportedEncodingException", e1.toString());
            e1.printStackTrace();
        } catch (ClientProtocolException e2) {
            Log.e("ClientProtocolException", e2.toString());
            e2.printStackTrace();
        } catch (IllegalStateException e3) {
            Log.e("IllegalStateException", e3.toString());
            e3.printStackTrace();
        } catch (IOException e4) {
            Log.e("IOException", e4.toString());
            e4.printStackTrace();
        }
        // Convert response to string using String Builder
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
            StringBuilder sBuilder = new StringBuilder();

            String line = null;
            while ((line = bReader.readLine()) != null) {
                sBuilder.append(line + "\n");
            }

            inputStream.close();
            result = sBuilder.toString();

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

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {

        //parse JSON data
        try {
            JSONArray jArray = new JSONArray(result);
            //JSONObject jObject = jArray.getJSONObject(0);

            String anem = jArray.getJSONObject(0).getString("PName");

                //String getname = jObject.getString("PName");
                //String getbrand = jObject.getString("Brand");
                tvname.setText(anem);
                //tvbrand.setText(getbrand);

            this.progressDialog.dismiss();
        } catch (JSONException e) {
            Log.e("JSONException", "Error: " + e.toString());
        }
    }
}//end of async

}//下课

任何帮助都将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    那不是数组,是对象

    JSONObject jObject = new JSONObject(result);
    String anem = jObject.getString("PName");
    tvname.setText(anem);
    
  2. # 2 楼答案

    {"Typ#":"1","Brand":"test","Cat#":"1","PName":"Test","PID":"2","Price":"120"} of type org.json.JSONObject cannot be converted to JSONArray.
    

    您试图将JSONObject转换为JSONArray,这是您的错误

    使用:

    JSONOjbect jso = new JSONObject(result);
    



    JSONObject以{开头,以}结尾

    JSONArray以[开始,以]结束