我想在我的Android应用程序中从我的PHP代码中获取JSON结果。 如果我需要所有数据库条目,它可以很好地工作:
public class GetAllFromOnlineDatabase extends AsyncTask<String, String, String> {
private static final String TAG = "GET_ALL";
// URL to get all stolpersteine
private static final String URL_ALL_STOLPERSTEINE = "http://.../get_all.php";
// JSON node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_STOLPERSTEINE = "stolpersteine";
private static final String TAG_PID = "pid";
private static final String TAG_TITLE = "title";
private static final String TAG_FIRST_NAME = "first_name";
private static final String TAG_LAST_NAME = "last_name";
// Creating JSON parser object
private JSONParser jParser = new JSONParser();
private MapControl mapControl;
public GetAllFromOnlineDatabase(MapControl mapControl) {
this.mapControl = mapControl;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
// Building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(URL_ALL_STOLPERSTEINE, "GET", params);
try {
// Checking for success TAG in JSON
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Getting array of products
JSONArray memory_list_stolpersteine_table_row = json.getJSONArray(TAG_STOLPERSTEINE);
// looping through all products
for (int i = 0; i < memory_list_stolpersteine_table_row.length(); i++) {
JSONObject jsonObject = memory_list_stolpersteine_table_row.getJSONObject(i);
// Storing each json item in variable
String id = jsonObject.getString(TAG_PID);
String title = jsonObject.getString(TAG_TITLE);
String first_name = jsonObject.getString(TAG_FIRST_NAME);
String last_name = jsonObject.getString(TAG_LAST_NAME);
}
} else {
Log.d(TAG, "Error - cannot read JSON");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
cancel(true);
}
}
但如果我使用另一个URL获取GET请求的详细信息,如下所示,它不起作用。 android中的json对象是空的。我怎样才能解决这个问题?感谢
private static final String URL_ALL_STOLPERSTEINE = "http://.../get_geopoint_by_id.php?pid=1";