Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从JSONURL中的jsonarray获取属性并在textview中显示_Java_Android_Json - Fatal编程技术网

Java 如何从JSONURL中的jsonarray获取属性并在textview中显示

Java 如何从JSONURL中的jsonarray获取属性并在textview中显示,java,android,json,Java,Android,Json,嗨,我正在尝试获取json响应,该响应具有jsonarray属性 比如(例如:{A:one,B:[{A:one,B:two},{A:two,B:one}])我一直在尝试 仅a:1和b:2值。但我的日志显示错误: RecyclerView:未连接适配器;跳过布局 Json分析错误:无法将java.lang.String类型的值自定义_属性转换为JSONArray 我想在textview中为我的产品详细信息视图获取此值 我的编码是: private class GetProductDetails e

嗨,我正在尝试获取json响应,该响应具有jsonarray属性

比如(例如:{A:one,B:[{A:one,B:two},{A:two,B:one}])我一直在尝试

仅a:1和b:2值。但我的日志显示错误:

  • RecyclerView:未连接适配器;跳过布局
  • Json分析错误:无法将java.lang.String类型的值自定义_属性转换为JSONArray
  • 我想在textview中为我的产品详细信息视图获取此值

    我的编码是:

    private class GetProductDetails extends AsyncTask<Void, Void, Void> {
    
    
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                dialog_pro = new ProgressDialog(Product_Detail_Activity.this);
                dialog_pro.setMessage("Please wait...");
                dialog_pro.setCancelable(false);
                dialog_pro.show();
    
            }
    
            @Override
            protected Void doInBackground(Void... arg0) {
                HttpHandler sh = new HttpHandler();
    
                // Making a request to url and getting response
                String jsonStr = sh.makeServiceCall(BaseURL);
    
                //Log.e(TAG, "Response from url: " + jsonStr);
    
                if (jsonStr != null) {
                    try {
                         JSONObject jsonObj = new JSONObject(jsonStr);
    
                        name = jsonObj.getString("name");
    
    
                        JSONArray items = new JSONArray("custom_attributes");
                        for (int i = 0; i < items.length(); i++) {
                            JSONObject c = items.getJSONObject(i);
    
                            String atrr = c.getString("attribute_code");
    
                            if(atrr.equalsIgnoreCase("short_description")) {
    
                                des = c.getString("value");
                            }
    
                        }
    
    
                    }catch (final JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "Json parsing error: " + e.getMessage(),
                                        Toast.LENGTH_LONG)
                                        .show();
                              //  pro_name.setText("Json error:" + e.getMessage());
    
                            }
                        });
    
                    }
                } else {
                    //Log.e(TAG, "Couldn't get json from server.");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Couldn't get json from server. Check LogCat for possible errors!",
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });
                    // AppController.getPermission().addToRequestQueue(jsonObj);
                }
    
                return null;
            }
    
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                /**
                 * Updating parsed JSON data into ListView
                 * */
                if (dialog_pro.isShowing())
                    dialog_pro.dismiss();
                pro_name.setText(name);
                short_desc.setText(des);
            }
    
        }
    
    私有类GetProductDetails扩展异步任务{
    @凌驾
    受保护的void onPreExecute(){
    super.onPreExecute();
    //显示进度对话框
    dialog\u pro=新进度对话框(产品详细信息活动,此项);
    对话框设置消息(“请稍候…”);
    对话框专业设置可取消(错误);
    dialog_pro.show();
    }
    @凌驾
    受保护的Void doInBackground(Void…arg0){
    HttpHandler sh=新的HttpHandler();
    //向url发出请求并获得响应
    字符串jsonStr=sh.makeServiceCall(BaseURL);
    //Log.e(标签,“来自url的响应:+jsonStr”);
    if(jsonStr!=null){
    试一试{
    JSONObject jsonObj=新的JSONObject(jsonStr);
    name=jsonObj.getString(“name”);
    JSONArray items=新的JSONArray(“自定义_属性”);
    对于(int i=0;i
    基于您的JSON

    try {
                JSONObject responseObject = new JSONObject(response);
                String A= responseObject.getString("A");
                JSONArray bArray= responseObject.getJSONArray("B");
                for(int i=0;i<bArray.length();i++){
                    JSONObject innerObject=bArray.getJSONObject(i);
                    String a= innerObject.getString("a");
                    String b= innerObject.getString("b");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    试试看{
    JSONObject responseObject=新的JSONObject(响应);
    字符串A=responseObject.getString(“A”);
    JSONArray bArray=responseObject.getJSONArray(“B”);
    
    对于(int i=0;i对于您的第一个问题RecyclerView:未连接适配器;跳过布局-> 只需先设置一个空适配器,在获得数据后立即更新它(这对我来说很有用) 对于第二个问题,Json解析错误:java.lang.String类型的值自定义_属性无法转换为JSONArray->

    您发布的例如json“{A:one,B:[{A:one,B:two},{A:two,B:one}]}”
    它不是有效的json格式

    请共享您的完整json响应