首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-Studio Java: Variable返回空

Android-Studio Java: Variable返回空
EN

Stack Overflow用户
提问于 2020-10-29 10:33:20
回答 1查看 51关注 0票数 0

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

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

代码语言:javascript
复制
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();
    }


}
EN

回答 1

Stack Overflow用户

发布于 2020-10-29 11:42:54

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64584138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档