簡體   English   中英

Android-Studio Java:變量返回空

[英]Android-Studio Java: Variable is returning empty

我試圖通過將其解析為 JSON 來顯示來自 mysql 數據庫的這些數據。 我能夠解析它並獲取我通過將其放在 Toast 上確認的數據。 但是不知何故,當我將它綁定到字符串時,當我使用該字符串設置一個 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();
    }


}

嘗試在 findviewbyid 之后調用 getJSON()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM