Я очень новичок в Android и пытаюсь создать программу, которая показывает вам результаты из базы данных, которую я имею.Поэтому, когда я набираю имя и база данных отправляет мне информацию об этом человеке.Однако, когда я смотрю на LogCat, он говорит: «09-09 22: 05: 39.544: ОШИБКА / log_tag (8813): Ошибка синтаксического анализа данных org.json.JSONException: Значение
Это мой код:
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_ENTER)
{
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://myIPaddress/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());
}
// At this point is should be set, if it isn't, tell user what went wrong
if (is != null) {
//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());
}} else {Log.i("log_tag", "Something went wrong!"//I don't know what to put here);} ;
et_Text.setText("");
//and clear the EditText control
return true;
}
}
return false;
}
});
}
}
Это мой php-код:
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_usersDB", $con);
$q=mysql_query("SELECT * FROM Persons WHERE FirstName='".$_REQUEST['name']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close($con);
?>
Вывод, который он анализирует, - это когда я ввожу "Eric", тогда он даст мне personID, равный 1, FirstName of Eric, LastName of (мой последнийимя), и 15 лет. Я не уверен, если вы просили об этом ...