0

Hi guys i am trying to do http requests i searched on internet and i found OkHttp library.

i am trying sample code:

OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url("http://www.vogella.com/index.html")
            .build();
    try {
        Response response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

but the app crashes, i tried to find the error and the reason it crashes is this line:

Response response = client.newCall(request).execute();

can anyone help me? if not can you suggest me another library for http requests? I have android 5.1(miui 8) and here are logcat errors: http://pastebin.com/5LtdxvpG line 42 is the line: Response response = client.newCall(request).execute();

  • Can you please share the error log – Abdelwahed Aug 06 '16 at 17:28
  • here it is: http://pastebin.com/5LtdxvpG line 42 that it says is: Response response = client.newCall(request).execute(); – IloveAndroid111 Aug 07 '16 at 12:01
  • old post.. but in my case, i use `enqueue` ```client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, final Response response) throws IOException { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } else { // do something wih the result } }``` – Nurkartiko May 20 '20 at 17:02

2 Answers2

1

Makes sure you have declared the Internet permission in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
Chait
  • 537
  • 6
  • 15
1

From what I see on your error log (in your comment), it is the well known "network on main thread" exception. It happens because Android prevents networking operations (i.e. your HTTP connection) on Main Thread.

Check this to see how you can fix it : How to fix android.os.NetworkOnMainThreadException?

Community
  • 1
  • 1
Abdelwahed
  • 1,694
  • 4
  • 21
  • 31
  • I edited like this: http://pastebin.com/uWML98At but i get error2:/ sorry i am new in android development – IloveAndroid111 Aug 07 '16 at 12:43
  • android.view.ViewRootImpl $CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views – IloveAndroid111 Aug 07 '16 at 12:58
  • Yes, you can't access your textview from another thread (must be the UI thread) . If you are looking for a quick (and dirty way :) ) to fix this : http://pastebin.com/5zDXtqun – Abdelwahed Aug 07 '16 at 13:03
  • No, now it wait to load the page and the app crash again..pff i edited the line: Response response = client.newCall(request).execute(); to: final Response response = client.newCall(request).execute(); because it throwed me error can you make an example code of okhttp and send me the code? – IloveAndroid111 Aug 07 '16 at 14:31
  • note that you are loading much data from the link you mentioned. Try to replce vogella.com with something simple (you can use my http://zuscholars.net/ ) for testing – Abdelwahed Aug 07 '16 at 14:41
  • @lloveAndroid111 Best of Luck :) – Abdelwahed Aug 07 '16 at 14:45
  • To make your code neat. I suggest you spend sometime understanding/using AsyncTask. It worths the effort ! – Abdelwahed Aug 07 '16 at 14:47
  • Okey thank you, one more question..do you suggest me an api to get e.g. the inner value of html tag? or doesnt exist? – IloveAndroid111 Aug 07 '16 at 14:50