本教程後面「https://www.youtube.com/watch?v=0Lr37suTPpg&list=PLsoBxH455yoZZeeza9TiG8I9dGP0zz5o9」我試圖解析我自己的JSON文件,它有一個JSONArray和多個JSONObjects,但不知何故AsyncTask有問題,我無法解決正常。如何解決這個由我的AsyncTask造成的崩潰
我的JSON文件的視圖:在第一線的
[
{
"type": "fuel",
"name": "Aral",
"address": "Siegmund Straße 12",
"lat": 49.8848387,
"lon": 8.6520691 },
{
"type": "amenity",
"name": "Hauptbahnhof",
"address": "Am Hauptbahnhof 20",
"lat": 49.8725,
"lon": 8.628889,
"icon": "bahnhof.jpg" },
{
"type": "amenity",
"name": "School",
"address": "Berliner Allee",
"lat": 49.8670679,
"lon": 8.6346578,
"icon": "lala_land.jpg" }
]
> --------- beginning of crash 06-20 09:23:57.039 3193-3213/com.example.schwarzerritter.parsingandlistviewadapter
> E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
> Process: com.example.schwarzerritter.parsingandlistviewadapter, PID:
> 3193
> java.lang.RuntimeException: An error occured while executing
> doInBackground()
> at android.os.AsyncTask$3.done(AsyncTask.java:304)
> at
> java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
> at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
> at java.util.concurrent.FutureTask.run(FutureTask.java:242)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818)
> Caused by: java.lang.SecurityException: Permission denied (missing
> INTERNET permission?)
> at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
> at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
> at java.net.InetAddress.getAllByName(InetAddress.java:215)
> at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
> at
> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
> at
> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
> at
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:48)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:41)
> at android.os.AsyncTask$2.call(AsyncTask.java:292)
> at java.util.concurrent.FutureTask.run(FutureTask.java:237)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818)
> Caused by: android.system.GaiException: android_getaddrinfo failed:
> EAI_NODATA (No address associated with hostname)
> at libcore.io.Posix.android_getaddrinfo(Native Method)
> at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
> at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
> at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
> at java.net.InetAddress.getAllByName(InetAddress.java:215)
> at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
> at
> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
> at
> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
> at
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:48)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:41)
> at android.os.AsyncTask$2.call(AsyncTask.java:292)
> at java.util.concurrent.FutureTask.run(FutureTask.java:237)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818)
> Caused by: android.system.ErrnoException: android_getaddrinfo failed:
> EACCES (Permission denied)
> at libcore.io.Posix.android_getaddrinfo(Native Method)
> at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
> at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
> at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
> at java.net.InetAddress.getAllByName(InetAddress.java:215)
> at
> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
> at
> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
> at
> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
> at
> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
> at
> org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:48)
> at
> com.example.schwarzerritter.parsingandlistviewadapter.MainActivity$LocationsAsyncTask.doInBackground(MainActivity.java:41)
> at android.os.AsyncTask$2.call(AsyncTask.java:292)
> at java.util.concurrent.FutureTask.run(FutureTask.java:237)
> at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
> at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
> at java.lang.Thread.run(Thread.java:818)
在
它說,我也許沒有給互聯網的權限,我絕對沒有。
這裏是AsyncClass:
public class LocationsAsyncTask extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... params){
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray("");
JSONObject jsonObject = new JSONObject(data);
for(int i=0; i< jsonArray.length();++i){
Locations location = new Locations();
JSONObject jRealObject = jsonArray.getJSONObject(i);
location.setName(jRealObject.getString("type"));
location.setName(jRealObject.getString("name"));
location.setName(jRealObject.getString("address"));
location.setName(jRealObject.getString("lat"));
location.setName(jRealObject.getString("lon"));
location.setImage(jRealObject.getString("icon"));
locationList.add(location);
}
return true;
}
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}catch (JSONException e){
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result){
super.onPostExecute(result);
if(result == false){
//msg
}else{
LocationsAdapter adapter = new LocationsAdapter(getApplicationContext(), R.layout.row, locationList);
list.setAdapter(adapter);
}
}
}
對於大家都在評論抱怨明顯的消息,我沒有給任何互聯網許可我的Android清單XML文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.schwarzerritter.parsingandlistviewadapter">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
順便說一下,您的錯誤日誌是可以格式化的。 –
你有沒有讀過異常堆棧跟蹤? '引發:java.lang.SecurityException:權限被拒絕(缺少 >互聯網權限?)' –
你們是否能夠閱讀我的問題,我提到我確實給了互聯網許可? –