1
votes

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'
2
Please post code in a text block. Do away with the image.greenapps

2 Answers

2
votes

Never execute longer running task on main thread specially during network calls where you have no idea when the response will come due to various factors like signal strength , server crash/busy etc.

Put your all network call either in Asynchtasks if they are relatively small like download image around 1-3 MB, doc/files etc or for longer tasks use HTTPUrlConncetion calls or you can use some networking libraries for better memory usage and optimizations , efficient cache mechanism.

Volley

  • Easy to use

  • Matured and backed by google

Retrofit

  • Has inbuild integration/support with useful libraries like

    • GSON - for parsing JSONObject/JSONArrays
    • Okio to support java.io and java.nio to make it much easier to access, store and process your data using ByteString and Buffer

    • RxJava - Observable patterns

though the above libraries like GSON etc can be used with Volley too as a separate dependancy.

There are many other open source libraries like Ion , OKHttp etc too, though the above are more popular but if you want your own customization then you can simply use HTTPUrlConncetion.

Try this link for networking call examples using threads and for more info about network libraries try comparison between OkHTTP, Retrofit, Volley .

There also some better options for Image downloading network calls i.e you can use Picasso , Glide , Fresco , Universal Image loader etc. try this link for image downloading libraries

1
votes

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);