昨天它工作正常,今天我得到這樣的消息:java.lang.String類型的值不能轉換爲JSONArray ....問題接受
這是
private class HttpAsyncTaskFamilies extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask
@Override
protected void onPostExecute(String result) {
JSONArray jArray;
String families = "";
Log.d(TAG, "result in onPostExectute Families -> " + result);
try {
DatabaseHelper helper = new DatabaseHelper(
getApplicationContext());
try {
helper.dropAndCreate(helper.getConnectionSource(),
Familia.class);
// Do the inserts in table Familia
jArray = new JSONArray(result);
Log.d(TAG, "jsonArray -> " + jArray);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
Log.d(TAG, "jsonObject -> " + json.toString());
String id = json.getString("familia_id");
Log.d(TAG, "id -> " + id);
int id_family = Integer.parseInt(json
.getString("familia_id"));
// for each Familia
Familia familia = new Familia(json.getInt("familia_id"),
json.getString("nom_familia"));
helper.getFamiliaDao().create(familia);
families += json.getString("nom_familia");
Log.d(TAG, "Familia inserida -> " + familia);
}
Log.d(TAG, "families actualitzades! " + families);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
的錯誤是在行:那我收到的JSON:當我嘗試從做一個JSONArray
和代碼將停止: jArray =新的JSONArray(結果);
任何想法?這是奇怪的是,昨天工作正常,今天我只能有這個錯誤...
找不到方法Add:
更多信息:
在onCreate方法我有:
new HttpAsyncTaskFamilies().execute(「http://aplicacionesmovilandroid.es/json/families.php」);
然後當HttpAsyncTaskFamilies()調用GET方法,這是一個方法:
public String GET(String url) {
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpClient = new DefaultHttpClient();
UtilDAOImpl dao = new UtilDAOImpl(this);
Versio versioObject = dao.lookupVersioCentre("centres");
String versio = versioObject.getVersio();
// we change the white space for %20, a representation of the white
// space
// that php can recognise. Without it, the php file always deceive a
// null value
// instead of the actual version
// String finalVersion = versio.replace(" ", "%20");
// make GET request to the given URL
// HttpResponse httpResponse = httpClient.execute(new HttpGet(url
// + "?centres_versio=" + finalVersion));
HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
// receive response as InputStream
inputStream = httpResponse.getEntity().getContent();
// convert InputStream to string
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
} else {
result = "No ha funcionat!";
}
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
郵政編碼不是照片。 –
你如何獲得'result'的值?在開始時可能是否有一些非打印字符? – Henry
@Chris查看我的回答 – Akshay