【问题标题】:Android-Studio Java: Variable is returning emptyAndroid-Studio Java:变量返回空
【发布时间】:2021-02-11 11:38:32
【问题描述】:

我正在尝试通过将 mysql 数据库中的数据解析为 JSON 来显示这些数据。我能够解析它并获取我通过将其放在 Toast 上确认的数据。 但不知何故,当我将它绑定到字符串时,当我使用字符串 setText 一个 TextView 时它又返回为空,我还通过使用 toast 确认它为空。我真的很困惑。

我是编程新手,我不太了解这些部分,尤其是我将 JSON 对象绑定到变​​量的方法。我正在尝试用谷歌搜索它,但我很难确定使用哪些术语来搜索,因为我只是不断获得相同的结果。我希望你能帮我找出我哪里出错了。

public class ArchiveFullDisplayActivity extends AppCompatActivity {
    methodsRepo methodsRepo = new methodsRepo(this);
    public static final String archivefullApi = "https://tesfas.000webhostapp.com/app/archivefullApi.php";

    TextView textTitle, textType, textId, textBody, textDate, textVnp, textFnf;
    ImageView ivImage;
    String title, type, img, vnp, fnf, body, date;
    int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archive_full_display);
        String dataID = getIntent().getStringExtra("dataID");
        Toast.makeText(this,dataID,Toast.LENGTH_SHORT).show();

        getJSON(archivefullApi);

        textBody = findViewById(R.id.tv_archivefull_body);
        textDate = findViewById(R.id.tv_archivefull_date);
        textFnf = findViewById(R.id.tv_archivefull_fnf);
        textVnp = findViewById(R.id.tv_archivefull_vnp);
        textId = findViewById(R.id.tv_archivefull_id);
        textTitle = findViewById(R.id.tv_archivefull_title);
        textType = findViewById(R.id.tv_archivefull_type);
        ivImage = findViewById(R.id.iv_archivefull_img);

        loadtoDisplay();
    }

    private void loadtoDisplay() {

        Toast.makeText(this,type,Toast.LENGTH_SHORT).show();
        textTitle.setText(title);

    }

    private void getJSON(final String urlWebService) {

        class GetJSON extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                //putting this toast to just incase i need to know if we're actually fetching the data from DB
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                try {
                    loadIntoArchivefullContents(s);
                }catch (JSONException e){

                }


            }

            //post requesting JSON object via dataID from the selected cardview list on ArchiveActivity
            @Override
            protected String doInBackground(Void... voids) {


                try {
                    String id = getIntent().getStringExtra("dataID");
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setRequestMethod("POST");
                    con.setDoOutput(true);
                    con.setDoInput(true);
                    OutputStream outputStream = con.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF -8"));
                    String post_data = URLEncoder.encode("id", "UTF-8")+"="+URLEncoder.encode(id, "UTF-8");
                    bufferedWriter.write(post_data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();

                    //StringBuilder object to read the string from the service
                    StringBuilder sb = new StringBuilder();

                    //We will use a buffered reader to read the string from service
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    //A simple string to read values from each line
                    String json;

                    //reading until we don't find null
                    while ((json = bufferedReader.readLine()) != null) {

                        //appending it to string builder
                        sb.append(json + "\n");
                    }

                    //finally returning the read string
                    return sb.toString().trim();
                } catch (Exception e) {
                    return null;
                }

            }
        }

        //creating asynctask object and executing it
        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    // parsing JSON data to String
    private void loadIntoArchivefullContents(String json)throws JSONException   {
        JSONArray contents = new JSONArray(json);
        for (int i = 0; i < contents.length(); i++) {
            JSONObject contentObject = contents.getJSONObject(i);

            id = contentObject.getInt("id");
            title = contentObject.getString("title");
            date = contentObject.getString("date");
            type = contentObject.getString("type");
            body = contentObject.getString("body");
            vnp = contentObject.getString("vnp");
            fnf = contentObject.getString("fnf");
            img = contentObject.getString("arc_img");

        }
    }

    public void btn_archivefull_dashboard(View view){
        methodsRepo.moveDashboard();
    }

    public void btn_archivefull_backtolist(View view){
        finish();
    }


}

【问题讨论】:

  • 不要默默地忽略异常catch (JSONException e){ }
  • @ScaryWombat 你是什么意思?
  • e.printStacktrace() - 有一个异常被忽略,打印出来。

标签: java android-studio variables


【解决方案1】:

尝试在 findviewbyid 之后调用 getJSON()。

【讨论】:

  • 还是一样。我仍然得到一个空变量。我不知道。但是您是否认为问题可能是因为我使用的是 JSONArray,即使我没有使用任何数组?我知道这听起来很愚蠢。大声笑对不起。你看我刚开始编程,我只通过谷歌和 youtube 学习。我从 youtube 上看到的回收商 cardview 方法中得到了我正在使用的这个数组方法。我试图搜索除 JSONArray 之外的其他方法,但我似乎找不到不使用的指南
猜你喜欢
  • 1970-01-01
  • 2019-10-05
  • 2021-07-28
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多