gpt4 book ai didi

android - 需要有关从数据库获取数据的帮助

转载 作者:行者123 更新时间:2023-11-30 23:35:59 24 4
gpt4 key购买 nike

我制作了一个程序,当一个人在编辑文本空间中键入一个名字时,它会显示我创建的数据库 (MySQL) 中具有该名字的人的信息。然而,当我输入 Eric 并按下方向键时,除了文本被删除(或重置)之外没有任何反应。我该怎么做才能修复它?

public class PS extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//get the two controls we created earlier, also with the resource reference and the id

final EditText et_Text = (EditText)findViewById(R.id.et_Text);

//add new KeyListener Callback (to record key input)
et_Text.setOnKeyListener(new OnKeyListener()
{
//function to invoke when a key is pressed
public boolean onKey(View v, int keyCode, KeyEvent event)
{
//check if there is
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
//check if the right key was pressed
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
InputStream is = null;
String result = "";


//the name data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name",et_Text.getText().toString()));

//http post

try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://*******/sampleDB/testSend.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}

is.close();

result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","PersonID: "+json_data.getInt("personID")+
", FirstName: "+json_data.getString("FirstName")+
", LastName: "+json_data.getString("LastName")+
", Age: "+json_data.getInt("Age")
);

}

}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
};




et_Text.setText("");
//and clear the EditText control
return true;
}

}

return false;
}
});

}
}

这是我在 Log Cat 中得到的:“09-06 21:03:16.375: ERROR/log_tag(22920): 解析数据 org.json.JSONException 时出错:值

最佳答案

日志中发生了什么?是否会引发任何异常?您正在清除行中的 EditText

 et_Text.setText("");

所以这可以解释为什么它被重置/清除

关于android - 需要有关从数据库获取数据的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com