我使用BufferedReader来获取JSON数据,但之后我只能显示其中一些数据。
使用字符集UTF-8时,我能够显示一个车辆数据,其他人只显示空值。
使用charset ISO-8859-1时,我能够显示两辆车的数据。
大多数数据只返回NULL,即使我知道它可用。例如,我无法显示有关此车辆的信息:http://apis.is/car?number=mm202但我始终能够显示有关此车辆的信息:http://apis.is/car?number=lj403。
这是我的AsyncTask来获取JSON数据:
protected class GetLicencePlateInfoTask extends AsyncTask<Void, Void, Void> {
protected String registryNumber, number, factoryNumber, type, subType, registeredAt, status, nextCheck, pollution, weight;
@Override
protected Void doInBackground(Void... params) {
// Ná í bílnúmerið úr ET
EditText licencePlateET = (EditText) findViewById(R.id.licencePlateEditText);
// Ná í bílnúmerið
String licencePlate = licencePlateET.getText().toString();
// Eyða óþarfa bilum
licencePlate = licencePlate.trim();
// Henda út bilum
licencePlate = licencePlate.replace(" ", "");
// Setja í uppercase
licencePlate = licencePlate.toUpperCase();
// Henda út bandstriki ef það er
if (licencePlate.contains("-")) {
licencePlate = licencePlate.replace("-", "");
}
// URL + bílnúmer
String carUrl = "http://apis.is/car?number=" + licencePlate;
// Virkar eins og browser
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost(carUrl);
httpPost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
// Geymir JSON gögnin sem við fáum til baka
String jsonString = null;
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// Json er UTF-8
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-1"), 8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
jsonString = stringBuilder.toString();
JSONObject jsonObject = new JSONObject(jsonString);
// Ná í bílnúmer úr results array
JSONArray resultsArray = jsonObject.getJSONArray("results");
for (int i = 0; i < resultsArray.length(); i++) {
try {
JSONObject oneObject = resultsArray.getJSONObject(i);
// Ná í info úr array
registryNumber = oneObject.getString("registryNumber");
number = oneObject.getString("number");
factoryNumber = oneObject.getString("factoryNumber");
type = oneObject.getString("type");
subType = oneObject.getString("subType");
registeredAt = oneObject.getString("registeredAt");
//status = oneObject.getString("status");
nextCheck = oneObject.getString("nextCheck");
pollution = oneObject.getString("pollution");
weight = oneObject.getString("weight");
} catch (JSONException e) {
e.printStackTrace();
}
}
// Log.i("registryNumber", registryNumber);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Log.i("jsonString", jsonString);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
displayInfoTV.setText(
"Tegund: " + type + "\n" +
"Undirtegund: " + subType + "\n" +
"Skráningarnúmer: " + registryNumber + "\n" +
"Fastanúmer: " + number + "\n" +
"Verksmiðjunúmer: " + factoryNumber + "\n" +
"Fyrst skráð: " + registeredAt + "\n" +
"CO2 losun: " + pollution + "\n" +
"Eiginþyngd: " + weight + "\n" +
//"Staða: " + status + "\n" +
"Næsta skoðun: " + nextCheck + "\n"
);
}
}
好的,我越来越了解发生了什么。
StringBuilder有时会返回无法发布:
jsonString: Cannot POST /car?number=DD550
但有时它有效::
jsonString: {"results":[{"registryNumber":"LJ403","number":"LJ403","factoryNumber"...
有什么想法吗?
答案 0 :(得分:1)
当我使用网络浏览器访问这些链接时,页面信息显示字符集是&#34; windows-1252&#34;。
尝试使用&#34; windows-1252&#34;或&#34; Cp1252&#34;作为InputStreamReader
的charset名称。更一般地,您可以修改代码以从响应头中提取要使用的字符集。 (它位于&#34; Content-Type&#34;标题...并且您需要从媒体类型字符串中提取它。这是HTTP规范中的一个示例。
Content-Type: text/html; charset=ISO-8859-4
如果此方法失败,那么您应该联系网站所有者并告诉他们他们发送的内容类型标头不正确或者他们的JSON有问题。
或者,您可以随机尝试Java理解的其他字符集。它们列在此处:http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html
答案 1 :(得分:-1)
使用
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}