以下是代码。代码工作得非常好。当EditText保持空白或具有mysql数据库中可用的某个字符串值时,它会显示内容。我的问题是,当editText中的输入与JSON对象或存储了Mysql查询结果的 returnString 不匹配时,我想显示错误消息解码 JSON 。
例如,如果输入='abi'//从edittext输入
khasi:abirt //khasi is column from the database with value abirt
输出:将显示khasi abirt
但是我希望输入与数据库的 khasi 列中的任何单词完全匹配时显示错误,而不是空白页面活动。
例如:input ='kljfldskfsldhf'
khasi column does not consist the input word
outout:空白页面活动
String result;
String returnString;// to store the result of MySQL query after decoding JSON
String input;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_meaning);
Intent intent = getIntent();
intent.setClass(DisplayMeaningActivity.this, MainActivity.class);
input =intent.getStringExtra(MainActivity.MEANING_INPUT);
tv = (TextView) findViewById(R.id.textView1);
// declare parameters that are passed to PHP script i.e. the name "meaning" and its value submitted by user
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// define the parameter
String response = null;
// call executeHttpPost method passing necessary parameters
try {
response = CustomHttpClient.executeHttpPost(
"http://kffg.netii.net/konnect.php?name="+input, // your ip address if using localhost server
postParameters);
// store the result returned by PHP script that runs MySQL query
String result = response.toString();
//parse json data
try{
returnString = "";
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID: "+json_data.getInt("ID")+
", Khasi: "+json_data.getString("Khasi")+
", English: "+json_data.getString("English")
);
//Get an output to the screen
returnString += "\n\n" + "Kyntien : " + json_data.getString("Khasi") + "\n"+ "Meaning: " + "\n"+ "" + json_data.getString("English");
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
答案 0 :(得分:0)
需要使用stringValue.contains("editTextvalue")
条件的if循环
它就像:
if (inputString.contains(columnNames)) {
//yes
} else {
//no, then print error
}
因为你已经有了#34;结果&#34;在String
中,这不应该是一个问题(?)。
没有完全理解数据库问题的列,但对于数据库的khasi列,您可以创建一个名称的arrayList,并将字符串与每个循环的a进行比较。
for(String string: columnNames){
if(input.equals(name)){}
}
维护一个arrayList,每次为khasi获取数据时都会添加字符串,如:
arrayList.add(json_data.getString("Khasi"));