Я знаю, что этот Вопрос повторяется, но я не могу найти ответ в Интернете.Android: String method return Null
02-10 11:03:28.465 22410-22767/com.eztimetable.eztimetable I/System.out﹕ ERROR : null 02-10 11:03:28.465 22410-22767/com.eztimetable.eztimetable I/System.out﹕ error 02-10 11:03:28.595 22410-22410/com.eztimetable.eztimetable E/log_tag﹕ Error parsing data org.json.JSONException: Value error of type java.lang.String cannot be converted to JSONArray
Почему я получаю ошибку/нуль?
Ниже приведен метод, который я использую
//in LecturerScreen class
public String getDataFromDB() {
try {
httpclient = new DefaultHttpClient();
httppost= new HttpPost("http://192.168.1.9/my_folder_inside_htdocs/retrieve.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("inputUser",inputUser.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost,responseHandler);
return response.trim();
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
return "error";
}
}
и это, как я вызываю метод
//in LecturerTimetable class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecturer_timetable);
tl = (TableLayout) findViewById(R.id.maintable);
final LecturerScreen getdb = new LecturerScreen();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<TimetableDisplay> timetable = parseJSON(data);
addData(timetable);
}
});
}
}).start();
}
Я не знаю, что где мой здесь не так. Может ли кто-нибудь дать мне решение по этому вопросу.
Я очень новичок в разработке Android-программы, поэтому, если бы кто-нибудь мог руководить, это было бы честью. Извините за плохой английский. Спасибо.
--UPDATE--
РНР скрипт
<?php
$hostname_localhost ="localhost";
$database_localhost ="eztimetable";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$lecturerUser = $_POST['inputUser'];
$query_search = "select lecturerCode from lectureraccount where lecturerId = '".$lecturerUser."'";
$query_exec = mysql_query($query_search,$localhost) or die(mysql_error());
while($row=mysql_fetch_assoc($query_exec))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
Вы ничего не возвращаете, когда блок try преуспевает. – Prudhvi
Привет, прудхи, что мне делать? можете ли вы дать какое-то решение? извините, я очень новичок в программировании. – SkyClasher
Неплохо, я вижу, что вы возвращаете 'response.trim()'. Но проверьте, соответствует ли ваш ответ null. Добавить 'Log.w ("String", response);' чуть ниже финального ответа на строку = httpclient.execute (httppost, responseHandler); и вставьте сообщение Log, которое вы получите. – Prudhvi