1

My app keep crashing in my JSON parser class. The thing is I am following a tutorial and it was working fine when it is mysql on localhost. now i am hosting it on my hostgator account. And since the code is lot of classes I put in lot of log identifiers to help me narrow down the exact place where the expection is being thrown. From the logcat I cant make sense of what the error is.

Code (JSON parser)

    package databasehandler;

/**
 * Created by YP on 26-Nov-14.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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 getJSONFromUrl(String url, List<NameValuePair> params) {
        Log.d("Chk","JSON parser function entered in JSON parser");

        // Making HTTP request
        try
        {
            Log.d("Chk","JSON parser try method entered in JSON parser");
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            Log.d("Chk","create new http client in JSON parser");
            HttpPost httpPost = new HttpPost(url);
            Log.d("Chk","new httppost url in JSON parser");
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            Log.d("Chk","new url encoded form entity in JSON parser");

            Log.d("Chk","new http response in JSON parser");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.d("Chk","new http response in JSON parser");
            HttpEntity httpEntity = httpResponse.getEntity();
            Log.d("Chk","new http entity in JSON parser");
            is = httpEntity.getContent();
            Log.d("Chk","end of try in JSON parser");

        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught unsupported exception in JSON parser");
        } catch (ClientProtocolException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught client protocol exception in JSON parser");
        } catch (IOException e)
        {
            e.printStackTrace();
            Log.d("Chk","caught IO exception in JSON parser");

        }

        try
        {
            Log.d("Chk","entered second try method in JSON parser");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            Log.d("Chk","entered second try method and before while in JSON parser");
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            Log.d("Chk","entered second try method after while in JSON parser");
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        }
        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;

    }
}

Logcat

11-30 13:15:06.548: D/Chk(2109): Login button event enetered
11-30 13:15:06.548: D/Chk(2109): JSON parser function entered in JSON parser
11-30 13:15:06.548: D/Chk(2109): JSON parser try method entered in JSON parser
11-30 13:15:06.548: D/Chk(2109): create new http client in JSON parser
11-30 13:15:06.548: D/Chk(2109): new httppost url in JSON parser
11-30 13:15:06.548: D/Chk(2109): new url encoded form entity in JSON parser
11-30 13:15:06.548: D/Chk(2109): new http response in JSON parser
11-30 13:15:06.551: D/AndroidRuntime(2109): Shutting down VM
11-30 13:15:06.554: W/AudioTrack(1218): AUDIO_OUTPUT_FLAG_FAST denied by client
11-30 13:15:06.562: E/AndroidRuntime(2109): FATAL EXCEPTION: main
11-30 13:15:06.562: E/AndroidRuntime(2109): Process: com.techiequickie.bharath.boadraf, PID: 2109
11-30 13:15:06.562: E/AndroidRuntime(2109): android.os.NetworkOnMainThreadException
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.net.InetAddress.getAllByName(InetAddress.java:215)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at databasehandler.JSONParser.getJSONFromUrl(JSONParser.java:55)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at databasehandler.UserFunctions.loginUser(UserFunctions.java:42)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.techiequickie.bharath.boadraf.Loginactivity$1.onClick(Loginactivity.java:93)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.view.View.performClick(View.java:4756)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.view.View$PerformClick.run(View.java:19749)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Handler.handleCallback(Handler.java:739)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.os.Looper.loop(Looper.java:135)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Native Method)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at java.lang.reflect.Method.invoke(Method.java:372)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-30 13:15:06.562: E/AndroidRuntime(2109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-30 13:15:06.568: W/ActivityManager(1218):   Force finishing activity com.techiequickie.bharath.boadraf/.Loginactivity
11-30 13:15:07.083: W/ActivityManager(1218): Activity pause timeout for ActivityRecord{3729739a u0 com.techiequickie.bharath.boadraf/.Loginactivity t79 f}
11-30 13:15:08.158: W/AudioTrack(1218): AUDIO_OUTPUT_FLAG_FAST denied by client
11-30 13:15:08.338: I/Process(2109): Sending signal. PID: 2109 SIG: 9
11-30 13:15:08.340: W/InputDispatcher(1218): channel '384fe924 com.techiequickie.bharath.boadraf/com.techiequickie.bharath.boadraf.WelcomeLogo (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
JackyBoi
  • 2,695
  • 12
  • 42
  • 74

1 Answers1

0

The problem is that you are accessing network functionality (making HTTP requests) on the main thread of your app. This is not a good practice in Android and in newer versions of Android it will throw this exception. The fix is to move your network operations (and any other long running or blocking operation) to another thread. You can use your own worker thread, a pool of threads or even AsyncTask to accomplish this.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • what would be the arguments for my case for ASYNC? – JackyBoi Nov 30 '14 at 16:01
  • From the looks of your existing code, I'd say a fully formed `Url`, `Void`, and `JSONObject`. – Larry Schiefer Nov 30 '14 at 19:06
  • Hi larry when u say fully formed URL u mean to say the URL itself? – JackyBoi Dec 01 '14 at 09:53
  • Right now you are taking a `Url` and adding parameters to it. You should be able to do this ahead of time. – Larry Schiefer Dec 01 '14 at 13:44
  • Hi Larry, I tot I almost had it. this is the tutorial that I am following so the code that u c their is pretty much my code. http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ so now where u think I need to add my ASYNC, and the above tutorial doesnot add it and it works just fine. – JackyBoi Dec 01 '14 at 14:38