-1

Error has appeared on //Response response = client.newCall(request).execute(); return response.body().string();// lines. The whole code: `

OkHttpClient client = new OkHttpClient();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }
}`

Where should I write url-adress which I enter? In OkHttp documentation it's shown only for public class. Where should I write this code if I wnat it in MainActivity:

public static void main(String[] args) throws IOException{ OkHttpexample okHttpexample = new OkHttpexample(); String response = okHttpexample.run("https://raw.github.com/square/okhttp/master/README.md"); System.out.println(response);}

If you know more detailed tutorials on OkHttp, it would be useful

Yusuf
  • 145
  • 6
  • 12
  • 1
    Is this a Java? Does it even complie? Do you know java at all? Why are you putting method's body inside other method's body? – Selvin Sep 08 '15 at 17:43

2 Answers2

2

For your reference:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

    new APIRequest().execute();
}    

private class APIRequest extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... voids) {
        String response;
        try {
            // HTTP GET
            GetExample example = new GetExample();
            response = example.run("http://192.168.1.100/api/getsomething");                
        } catch (IOException e) {
            response = e.toString();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        // do something...
    }
}

public class GetExample {
    final OkHttpClient client = new OkHttpClient();

    String run(String url) throws IOException {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();                
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {                
            return e.toString();
        }
    }
}    
}
BNK
  • 23,994
  • 8
  • 77
  • 87
1

You may need to catch the IOException, try involving your HTTP call with a Try/Catch.

Try {
    Request request = new Request.Builder() 
            .url(url)
            .build(); 
    Response response = client.newCall(request).execute();
    return response.body().string();
} catch (IOException exception) {
}

and as you do this, remove the throws IOException from your method declaration.

Mateus Brandao
  • 900
  • 5
  • 9
  • NO for empty catch and of course NO for NOMTException – Selvin Sep 08 '15 at 17:47
  • Wow, really? What to do in the catch is for him to decide... of course you shouldnt leave it empty. I didn't get what you meant by "NOMTExeption" thought.. Could you be more clear? – Mateus Brandao Sep 08 '15 at 17:49
  • Not empty catch is for him to ask yet another annoying question about NOMTE - Most common exception in android's world when noobs working with networking – Selvin Sep 08 '15 at 17:50
  • I' m not... not for all. Only for those that are too s*d to use Google before asking... Fx this code is from first site when you try to search okhttp in google.. – Selvin Sep 08 '15 at 17:56