編輯 我解決我的問題:剛剛更換mysql_query('set names utf8');
與mysql_set_charset('utf8',$con);
($ con是我的 「的mysql_connect」)Android JSON請求:由「utf8」引起的應用程序崩潰?
因爲我在PHP的文件添加
mysql_query('set names utf8');
我的Andorid應用崩潰。在此之前,所有工作正常,但我得到了一個「空」 - 問題與我的JSON請求「非洲統一組織」,所以我需要添加「設置名稱UTF8」在我的PHP的java請求
Android的錯誤日誌:
Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
這裏我虛空中我Actitity類:
public void setTextToTextViewText(JSONArray jsonArray) {
String s = "";
JSONObject json = null;
try {
json = jsonArray.getJSONObject(0);
s = json.getString("Eventbeschr") + "\n" + "\n" +
"Spiel: " + json.getInt("Spiel") + "\n" +
"Beginn: " + json.getString("EventDatum") + "\n" +
"Max. Teilnehmer: " + json.getInt("maxTeilnehmer") + "\n" +
"\n";
} catch (JSONException e) {
e.printStackTrace();
this.textView_eventTitel.setText(R.string.eventinfo_getting_text_error_body);
}
this.textView_eventInfo.setText(s);
}
private class GetLastEventTask extends AsyncTask<ApiConnector, Long, JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetLatestEvent();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
setTextToTextViewHead(jsonArray);
}
}
在我的onCreate,我呼籲:
new GetLastEventTask().execute(new ApiConnector());
我ApiConnector.java文件
public class ApiConnector {
public JSONArray GetLatestEvent()
{
String url = "MY URL TO PHP FILE";
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
莫比它的幫助,但這裏也是主要的一塊我的PHP文件:
mysql_select_db("DB NAME", $con);
$result=mysql_query('set names utf8');
$result = mysql_query("SELECT * FROM event ORDER BY EventID desc limit 1");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
似乎jsonArray是空 – Rohit5k2
@ Rohit5k2在我的瀏覽器我看到輸出,所以這工作正常。 – DominikTV