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 9167227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:20:18+00:00 2026-06-17T15:20:18+00:00

I have problems about json in android I currently have following code from 2

  • 0

I have problems about json in android
I currently have following code from 2 classes which are JSONParser.java dan Tab1.java for login function.
this will work like charm in android 2.3.3 and below, but it not work if above it. (honeycomb, ICS, JB)
I have googling about this problem and I found that it has something to do with asynctask, but I still don’t understand how to implement it in my code.
Sorry for asking newbie question, I appreciate your help
thanks before.

JSONParser.java

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;

    }
}

here is the login class

Tab1.java

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Tab1 extends Activity {
    String var_usr,var_pass;
    EditText usr, psw;
    JSONArray str_login = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.panel_login);

        usr = (EditText) findViewById(R.id.txt_username);
        psw = (EditText) findViewById(R.id.txt_pass);


        Button submit = (Button) findViewById(R.id.btn_login);
        submit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                var_usr = usr.getText().toString();
                var_pass = psw.getText().toString();
                String stts = "";
                String kode = "";
                String link_url = "http://10.0.2.2/gollege/login.php?usr="+var_usr+"&psw="+var_pass;
                com.bucheuw.gollege.portal.JSONParser jParser = new com.bucheuw.gollege.portal.JSONParser();
                JSONObject json = jParser.AmbilJson(link_url);

                try {
                    str_login = json.getJSONArray("statuslogin");

                    for(int i = 0; i < str_login.length(); i++){
                        JSONObject ar = str_login.getJSONObject(i);
                        TextView st = (TextView) findViewById(R.id.txt_alert);
                        String alrt = ar.getString("hasil");
                        stts = ar.getString("st");
                        kode = ar.getString("id");
                        st.setText(alrt);
                        usr.setText("");
                        psw.setText("");

                        if(stts.trim().equals("ok"))
                        {
                            Intent ni = null;                           
                            ni = new Intent(Tab1.this, com.bucheuw.gollege.portal.DashboardActivity.class);
                            Bundle b = new Bundle();
                            b.putString("par_kode", kode);
                            ni.putExtras(b);
                            startActivity(ni);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }



}

logcat error lines

01-21 18:25:57.016: E/AndroidRuntime(533): FATAL EXCEPTION: main
01-21 18:25:57.016: E/AndroidRuntime(533): android.os.NetworkOnMainThreadException
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-21 18:25:57.016: E/AndroidRuntime(533):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-21 18:25:57.016: E/AndroidRuntime(533):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-21 18:25:57.016: E/AndroidRuntime(533):  at libcore.io.IoBridge.connect(IoBridge.java:112)
01-21 18:25:57.016: E/AndroidRuntime(533):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-21 18:25:57.016: E/AndroidRuntime(533):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-21 18:25:57.016: E/AndroidRuntime(533):  at java.net.Socket.connect(Socket.java:842)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-21 18:25:57.016: E/AndroidRuntime(533):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-21 18:25:57.016: E/AndroidRuntime(533):  at com.bucheuw.gollege.portal.JSONParser.AmbilJson(JSONParser.java:38)
01-21 18:25:57.016: E/AndroidRuntime(533):  at com.bucheuw.gollege.Tab1$1.onClick(Tab1.java:51)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.view.View.performClick(View.java:3511)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.view.View$PerformClick.run(View.java:14105)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.os.Handler.handleCallback(Handler.java:605)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.os.Looper.loop(Looper.java:137)
01-21 18:25:57.016: E/AndroidRuntime(533):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-21 18:25:57.016: E/AndroidRuntime(533):  at java.lang.reflect.Method.invokeNative(Native Method)
01-21 18:25:57.016: E/AndroidRuntime(533):  at java.lang.reflect.Method.invoke(Method.java:511)
01-21 18:25:57.016: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-21 18:25:57.016: E/AndroidRuntime(533):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-21 18:25:57.016: E/AndroidRuntime(533):  at dalvik.system.NativeStart.main(Native Method)
  • 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-17T15:20:19+00:00Added an answer on June 17, 2026 at 3:20 pm

    That’s because in later versions you cannot do a network connection in a main thread..
    To use an AsyncTask..

    private class MyTask extends AsyncTask {

       private Context context;
    
       public MyTask(Context context) {
         this.context = context;
       }
    
       protected void onPreExecute() {
          progressDialog = ProgressDialog.show(context, "", "msg", true); 
       }
    
      protected Long doInBackground(URL... urls) {
           //add here your json part
      }
    
     protected void onPostExecute(Long result) {
         progressDialog.dismiss();
    
         Intent ni = null;                           
                            ni = new Intent(Tab1.this, com.bucheuw.gollege.portal.DashboardActivity.class);
                            Bundle b = new Bundle();
                            b.putString("par_kode", kode);
                            ni.putExtras(b);
                            startActivity(ni);  
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario: I am parsing a JSON Response from a server
I read about JSON from internet but still i have not got the grasp
I have an android application which downloads its information as JSON. A typical JSON
We have some performance problems with one of our applications. I thought about using
I have read about a lot of people having problems with the browser not
I have quite a few problems, probably misunderstandings, about the new keyword with properties
i am haveing some problems with JSON and arrays. I have been messing around
I have various questions about setTimeout: - In my code, I clean timeouts with
We have two systems: external and internal, which are sharing information in JSON format
I have a question about cloning within a loop, two problems actually and im

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.