简体   繁体   中英

Android-Studio Java: Variable is returning empty

i am trying to display this data from a mysql database by parsing it to JSON. i am able to parse it and fetch the data that i confirmed by putting it on a Toast. but somehow when i bind it to string it comes back empty when i use the string to setText a TextView, i also confirmed that its empty by using toast. i am really confused.

i am new to programming i don't understand these parts that much, especially the method where i bind the JSON objects to variables. I am trying to google it but i was having a hard time on which terms to use to search since i just keep on getting the same results. i hope you can help me find where i went wrong.

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()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM