2014-12-22 33 views
1

編輯 我解決我的問題:剛剛更換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); 
+0

似乎jsonArray是空 – Rohit5k2

+0

@ Rohit5k2在我的瀏覽器我看到輸出,所以這工作正常。 – DominikTV

回答

0

我發現,當我組裝陣列,然後json_encode他們必須對數組中的值進行utf8編碼,以防止偶爾發生錯誤。

嘗試:

$output[] = utf8_encode($row); 

編輯:

對不起,不沒收$行是在第一次讀的數組。爲此,你必須將你的數據集更加零散地構建出來,你可以通過增加一個鍵來手動添加你想要的每個字段到數組中(很明顯用你的真實名字替換佔位符字段名)。

$key = 0; 
while($row = mysql_fetch_assoc($result)) 
{ 
    $output[$key]['field1'] = utf8_encode($row['field1']); 
    $output[$key]['field2'] = utf8_encode($row['field2']); 
    $key++; 
} 

另一種選擇可能是對我們像array_map功能做已經裝配的陣列上的編碼:

while($row = mysql_fetch_assoc($result)) 
{ 
    $output[]=$row; 
} 
$output = array_map("utf8_encode", $output); 
+0

我得到這個錯誤:'警告:utf8_encode()期望參數1是字符串,在/var/customers/webs/ni161189_1/json-sql/getLatestEvent.php中給出的數組在第18行[null]' 第18行是你的編碼你的答案 – DominikTV