[英]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.