本教程后面跟着“https://www.youtube.com/watch?v=0Lr37suTPpg&list=PLsoBxH455yoZZeeza9TiG8I9dGP0zz5o9”我试图解析我自己的JSON文件,里面有一个带有多个JSONObjects的JSONArray,但不知道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>
答案 0 :(得分:2)
您的错误很简单,missing INTERNET permission?
,这意味着您需要向您的应用程序授予Internet权限。将以下内容添加到清单中:
<uses-permission android:name="android.permission.INTERNET" />