Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:44:03+00:00 2026-06-13T15:44:03+00:00

I am a new in android and I am trying to display data that

  • 0

I am a new in android and
I am trying to display data that I am getting from server through json.I use lazylist with JSONparser.my app its work on android 2.3 but on 4.0 ..so i trying to add AsyncTask in my code.
And my code is:

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;

    }
}

when i run in 4.0,its error

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

I dont know where i must put AsyncTask
Can anyone helps me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:44:04+00:00Added an answer on June 13, 2026 at 3:44 pm

    1.First you define class and create object of class

     MyFirstMethod myfirst;
    
    1. Now you execute this class adding

    myfirst=new MyFirstMethod();

    myfirst.execute(your url);

    public class MyFirstMethod extends AsyncTask<String, Void, String> {
                ProgressDialog dialog;
    
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    dialog = new ProgressDialog(context);
                    dialog.setMessage("Loading....");
                    dialog.show();
                }
    
                @Override
                protected String doInBackground(String... params) {
                    String url = params[0];
                    try {
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(url);
                        ResponseHandler<String> resHandler = new  BasicResponseHandler();
                        String page = httpClient.execute(httpGet, resHandler);
                        Log.d("result", page);
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(String result) {
    
                    super.onPostExecute(result);
                    dialog.dismiss();
                    dialog.cancel();
    
    
                }
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make an app that parses data from Twitter using JSON
I'm making a new Android app that essentially mirrors data available on our website.
I am trying to make an android app that retrieves info from google app
im new to android and I've been trying to figure out how to display
i am new to android programming and i am trying to develop an app
I am new to android programming and i am trying to develop this app
I am new to android and I am trying to load huge data which
I'm trying to create a list adapter that pulls in and displays data from
I'm trying to start off my Android app journey with an app to display
I'm new to android and I'm trying to make a simple android program that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.