概述
我试图分析我的Android应用程序中的JSON(进一步我将填补与内容列表视图),所以我做了这个类:Android的JSON异常而解析
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReadJSONFeedTask().execute("http://10.14.182.253/viztonjob/update.php?updatedAt=01011970");
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask {
private final ProgressDialog pd = new ProgressDialog(MainActivity.this);
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPreExecute() {
this.pd.setMessage("Chargement de la liste des métiers ...");
this.pd.show();
}
protected void onPostExecute(String result) {
pd.dismiss();
try {
JSONArray jsonArray = new JSONArray(result);
Log.i("JSON",
"Number of surveys in feed: " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Toast.makeText(
getBaseContext(),
jsonObject.getString("metiers") + " - "
+ jsonObject.getString("categorie"),
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
所以我m使用AsyncTask来恢复我的数据,然后我尝试在Toast中显示它。
当我启动我的申请,我得到我的ProgressDialog后关闭以下警告:
W/System.err(13382): org.json.JSONException: Value {"chambres":[{"adresse1":"23 allée des Fontainiers","adresse2":"BP 125","fax":"04 92 32 17 75","codePostal":"04004","email":"[email protected]","ndepartement":"04","tel":"04 92 30 90 90","ville":"DIGNE-LES-BAINS","urlSite":"http://www.cm-04.fr","nom":"CMA des Alpes-de-Haute-Provence"},{"adresse1":"Square Voltaire","adresse2":"","fax":"04 92 53 48 85","codePostal":"05000","email":"[email protected]","ndepartement":"05","tel":"04 92 51 06 89","ville":"GAP","urlSite":"http://www.cm-gap.fr","nom":"CMA des Hautes-Alpes"},{"adresse1":"110, avenue de Verdun","adresse2":"","fax":"04 93 31 07 56","codePostal":"06700","email":"[email protected]","ndepartement":"06","tel":"04 93 14 16 14","ville":"ST-LAURENT-DU-VAR","urlSite":"http://www.cm-alpesmaritimes.fr","nom":"CMA des Alpes-Maritimes"},{"adresse1":"8 rue de Clèves","adresse2":"BP 290","fax":"03 24 56 81 88","codePostal":"08014","email":"[email protected]","ndepartement":"08","tel":"03 24 56 81 81","ville":"CHARLEVILLE-MEZIERES","urlSite":"http://www.cma-ardennes.fr","nom":"CMA des Ardennes"},{"adresse1":"5 boulevard Pèbre","adresse2":"","fax":"04 91 32 24 25","codePostal":"13295","email":"[email protected]","ndepartement":"13","tel":"04 91 32 24 24","ville":"MARSEILLE","urlSite":"http://www.cma13.fr","nom":"CMA des Bouches du Rhône"},{"adresse1":"2 rue Claude-Bloch","adresse2":"BP 5059","fax":"02 31 53 25 01","codePostal":"14077","email":"[email protected]","ndepartement":"14","tel":"02 31 53 25 00","ville":"CAEN","urlSite":"http://www.cmar-bn.fr","nom":"CMAR de Basse-Normandie : section du Calvados"},{"adresse1":"45 avenue de la République","adresse2":"BP 511","fax":"04 71 45 65 10","codePostal":"15005","email":"[email protected]","ndepartement":"15","tel":"04 71 45 65 00","ville":"AURILLAC","urlSite":"http://www.cma-cantal.fr","nom":"CMA du Cantal"},{"adresse1":"68 avenue Gambetta","adresse2":"","fax":"05 45 90 47 29","codePostal":"16021","email":"[email protected]","ndepartement":"16","tel":"05 45 90 47 00","ville":"ANGOULEME","urlSite":"http://www.cma-charente.fr","nom":"CMA de la Charente"},{"adresse1":"107, avenue Michel-Crépeau","adresse2":"","fax":"05 46 50 00 09","codePostal":"17024","email":"[email protected]","ndepartement":"17","tel":"05 46 50 00 00","ville":"LA ROCHELLE","urlSite":"http://www.cm-larochelle.fr","nom":"CMA de la Charente-Maritime"},{"adresse1":"40 rue Moyenne","adresse2":"BP 249","fax":"02 48 69 70 69","codePostal":"18005","email":"[email protected]","ndepartement":"18","tel":"02 48 69 70 71","ville":"BOURGES","urlSite":"http://www.cm-18.fr","nom":"CMA du Cher"},{"adresse1":"Immeuble Jean-Marie-Saute","adresse2":"8 avenue Alsace-Lorraine","fax":"05 55 20 19 14","codePostal":"19002","email":"[email protected]","ndepartement":"19","tel":"05 55 29 95 95","ville":"TULLE","urlSite":"http://www.cma-correze.fr","nom":"CMA de la Corrèze"},{"adresse1":"5-7 rue de Londres","adresse2":"BP 49","fax":"05 55 51 95 39","codePostal":"23011","email":"[email protected]","ndepartement":"23","tel":"05 55 51 95 30","ville":"GUERET","urlSite":"http://www.crma-limousin.fr","nom":"CMA de la Creuse"},{"adresse1":"Créavallée Nord","adresse2":"Boulevard des saveurs","fax":"05 53 45 15 08","codePostal":"24660","email":"[email protected]","ndepartement":"24","tel":"05 53 35 87 00","ville":"COULOUNIEIX CHAMIERS","urlSite":"http://www.artisanat24.com/","nom":"CMA de la Dordogne"},{"adresse1":"21 rue de la Préfecture","adresse2":"BP 75","fax":"03 81 21 35 00","codePostal":"25013","email":"[email protected]","ndepartement":"25","tel":"03 81 21 35 35","ville":"BESANCON","urlSite":"http://www.cma-doubs.fr","nom":"CMA du Doubs"},{"adresse1":"Avenue Adolphe-Figuet","adresse2":"BP 153-Le clos des Tanneurs","fax":"04 75 02 73 94","codePostal":"26104","email":"[email protected]","ndepartement":"26","tel":"04 75 48 72 00","ville":"ROMANS","urlSite":"http://www.cma-drome.fr","nom":"CMA de la Drôme"},{"adres
W/System.err(13382): at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err(13382): at org.json.JSONArray.(JSONArray.java:91)
W/System.err(13382): at org.json.JSONArray.(JSONArray.java:103)
...
,并没有任何敬酒。我试图验证JSON,并明显没有错误。所以我想知道你是否知道它可以从哪里来。
2013-04-03
kom
+0
查了一下我公顷已经说了一个相关的主题[这里](http://stackoverflow.com/questions/14749449/parsing-french-text-with-simple-framework-not-working/14753351#14753351) - 流字符编码是重要的。 –
最后
以上就是单纯微笑为你收集整理的android解析json异常处理,Android的JSON异常而解析的全部内容,希望文章能够帮你解决android解析json异常处理,Android的JSON异常而解析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复