我是android的新手,我正在尝试显示我通过json从服务器获取的数据。我使用lazylist和JSONparser。我的应用程序在android 2.3上工作,但在4.0上工作......所以我试图在我的代码中添加AsyncTask。我的代码是:
package dlmbg.pckg.tampil.gambar;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject AmbilJson(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
当我在 4.0 中运行时,它的错误
11-01 12:16:19.063: E/AndroidRuntime(623): FATAL EXCEPTION: main
11-01 12:16:19.063: E/AndroidRuntime(623): java.lang.RuntimeException: Unable to start activity ComponentInfo{dlmbg.pckg.tampil.gambar/dlmbg.pckg.tampil.gambar.TampilkanGambarActivity}: android.os.NetworkOnMainThreadException
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-01 12:16:19.063: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:16:19.063: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
11-01 12:16:19.063: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-01 12:16:19.063: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-01 12:16:19.063: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
11-01 12:16:19.063: E/AndroidRuntime(623): Caused by: android.os.NetworkOnMainThreadException
11-01 12:16:19.063: E/AndroidRuntime(623): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-01 12:16:19.063: E/AndroidRuntime(623): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-01 12:16:19.063: E/AndroidRuntime(623): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-01 12:16:19.063: E/AndroidRuntime(623): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-01 12:16:19.063: E/AndroidRuntime(623): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-01 12:16:19.063: E/AndroidRuntime(623): at dlmbg.pckg.tampil.gambar.JSONParser.AmbilJson(JSONParser.java:38)
11-01 12:16:19.063: E/AndroidRuntime(623): at dlmbg.pckg.tampil.gambar.TampilkanGambarActivity.onCreate(TampilkanGambarActivity.java:38)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.Activity.performCreate(Activity.java:4465)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-01 12:16:19.063: E/AndroidRuntime(623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-01 12:16:19.063: E/AndroidRuntime(623): ... 11 more
我不知道我必须把 AsyncTask 放在哪里有人可以帮助我吗?