Need help on getting data from database
I made a program that when one types a first name in the edit text space, it shows the information of the person with that first name from a database (MySQL) that I created. However, when I type in Eric and press the D-pad, nothing happens except that the text is erased(or reset). What can I do to fix it?
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 r开发者_运维技巧ecord 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;
}
});
}
}
This is what I'm getting in the Log Cat: "09-06 21:03:16.375: ERROR/log_tag(22920): Error parsing data org.json.JSONException: Value
Whats happening in the log? Are any exceptions getting raised? You are clearing the your EditText in the line
et_Text.setText("");
So that would account for why its getting reset/cleared
精彩评论