我正在尝试从json获取数据并在android中的textview中显示它。这是我之前的问题:Display Student details in text view after validation in android这是我在IMEI_Val.java中的代码:
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(IMEI_Val.this,"Please Wait",null,true,true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.contains("Success")){
Intent i = new Intent(IMEI_Val.this, CapturePhoto.class);
try {
JSONObject responseObject = new JSONObject();
JSONArray canData = responseObject.getJSONArray("can_data");
JSONObject canDataJSONObject = canData.getJSONObject(0);
String name = canDataJSONObject.getString("name");
String father_name = canDataJSONObject.getString("father_name");
String sex = canDataJSONObject.getString("sex");
i.putExtra("name",name);
i.putExtra("father_name",father_name);
i.putExtra("sex",sex);
startActivity(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
Log.d("Tag Name", "Log Message");
}
}
这是我想要显示细节的另一个活动:
private void getData() {
textNameVal=(TextView)findViewById(R.id.textNameVal);
textYearVal=(TextView)findViewById(R.id.textYearVal);
String name = getIntent().getStringExtra("name");
String father_name = getIntent().getStringExtra("father_name");
String sex = getIntent().getStringExtra("sex");
textNameVal.setText("Candidate Name:\t" + name);
textYearVal.setText("SEX:\t" + sex);
textParentVal.setText("Father's NAme:\t" + father_name);
}
当我运行此代码时,如果我输入有效输入,则不会使用CapturePhoto.class。任何人都可以帮我解决。对于错误的输入,它显示的信息是“不存在"在吐司。但是对于有效的输入,它只显示对话为"请等待"并没有进入下一个活动。
答案 0 :(得分:0)
你的onPostExecute会抛出JSONExeption
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
if(s.contains("Success")){
Intent i = new Intent(IMEI_Val.this, CapturePhoto.class);
try {
JSONObject responseObject = new JSONObject();
JSONArray canData = responseObject.getJSONArray("can_data");
JSONObject canDataJSONObject = canData.getJSONObject(0);
String name = canDataJSONObject.getString("name");
String father_name = canDataJSONObject.getString("father_name");
String sex = canDataJSONObject.getString("sex");
i.putExtra("name",name);
i.putExtra("father_name",father_name);
i.putExtra("sex",sex);
startActivity(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
Log.d("Tag Name", "Log Message");
}
}
responseObject=new JSONObject();
创建一个新的空对象,但需要
responseObject=new JSONObject(s);
然后它将有来自响应的值。
答案 1 :(得分:-1)
尝试将意图置于try {} block
中