Thursday, 4 August 2016

Android Studio 2.1.2 http GET error




I have been fighting with this for 2 days now, end of the line for me. It seems I am having trouble in getting my makeHttpRequest. I can add data, view all records in a table but I just can't seem to get a single record data to my page, app crashes completely "Unfortunately 'app_name" has stopped.



Below is my JSonParser file and the Activity file. Any suggestions will be welcome. I have read through tons of similar questions but none seemed to have worked...



Thanks all.



Errors in JsonParser as per image 'JSonParser, no errors in my Activity file as per image 'Activity'JSonParser



Activity




Log error as follow ---




FATAL EXCEPTION: main
Process: ...MyApp.co.za.androidtophp, PID: 32447
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
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:185)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:172)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:130)
at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1337)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:705)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:578)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:494)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:472)
at ...MyApp.co.za.androidtophp.JSONParser.makeHttpRequest(JSONParser.java:63)

at ...MyApp.co.za.androidtophp.EditProductActivity$GetProductDetails$1.run(EditProductActivity.java:134)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)





Gradle files is all in there as far as apache is concerned -




  1. useLibrary 'org.apache.http.legacy'

  2. compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.volley:volley:1.0.0'

    compile 'com.google.code.gson:gson:2.6.2'
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'


Answer



That is happening because you are trying to make the HTTP Request synchronous. Please create an AsyncTask or disable strict mode (only clever when there's no problem when the app could hange some seconds).



Warning: Don't use this for public app releases!!!



For disabling Strict Mode, add following code in your onCreate() (right after super.onCreate(savedInstanceState))




    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...