Skip to content Skip to sidebar Skip to footer

Android Retrieving Json Object From Url

I am working on an app that makes an API call to a php script that echos a JSON Object. Testing the php file manually through a browser returns the expected information, but my app

Solution 1:

Have you tried OkHttp.

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

You can try following code:

package com.squareup.okhttp.guide;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;

publicclassGetExample {
  OkHttpClientclient=newOkHttpClient();

  String run(String url)throws IOException {
    Requestrequest=newRequest.Builder()
        .url(url)
        .build();

    Responseresponse= client.newCall(request).execute();
    return response.body().string();
  }

  publicstaticvoidmain(String[] args)throws IOException {
    GetExampleexample=newGetExample();
    Stringresponse= example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}

For more you can visit:

Vogella's article

OkHttp 2.0

Post a Comment for "Android Retrieving Json Object From Url"