I have given the dependencies in my project gradle file which are given below.
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
I am getting the exception android.os.NetwokOnMainThreadException
I am not able to get how can i solve that issue because i alreday review the OKHTTP recipes form given below link. https://github.com/square/okhttp/wiki/Recipes
public class MainActivity extends AppCompatActivity {
private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Request request = new Request.Builder()
.url("https://api.github.com/gists/c2a7c39532239ff261be")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
Toast.makeText(getApplicationContext(),"false",Toast.LENGTH_LONG).show();
Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
Toast.makeText(getApplicationContext(),entry.getKey().toString(),Toast.LENGTH_LONG).show();
}
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
static class Gist {
Map<String, GistFile> files;
}
static class GistFile {
String content;
}
}