【问题标题】:Android JSON request: App crashes caused by "utf8"?Android JSON请求:“utf8”导致的应用程序崩溃?
【发布时间】:2014-12-22 22:17:39
【问题描述】:

编辑 我解决了我的问题:刚刚将mysql_query('set names utf8'); 替换为mysql_set_charset('utf8',$con);($con 是我的“mysql_connect”)


自从我添加到我的 PHP 文件中

mysql_query('set names utf8');

我的 Andorid 应用程序崩溃。在此之前,一切正常,但我的 JSON 请求中出现“öäü”的“null”问题,因此我需要在我的 php java 请求中添加 'set names utf8'

Android 错误日志:

 Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference

这是我在活动课上的空白:

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 在我的浏览器中我看到了输出,所以这工作正常。

标签: php android json request


【解决方案1】:

我发现当我组装数组然后对它们进行 json_encode 时,我必须对数组中的值进行 utf8_encode 以防止它抛出偶尔的错误。

试试:

$output[] = utf8_encode($row);

编辑:

很抱歉在第一次读取时没有发现 $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);

【讨论】:

  • 我收到此错误:'警告:utf8_encode() 期望参数 1 为字符串,数组在 /var/customers/webs/ni161189_1/json-sql/getLatestEvent.php 第 18 行 [null ]' 第 18 行是你的代码你的答案
猜你喜欢
  • 2014-05-08
  • 2013-12-02
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 2020-11-16
  • 2018-01-29
  • 2018-09-23
相关资源
最近更新 更多