I'm trying get a JSONObject of a Web Service. To do this I'm using HttpClient with HttpResponse. When web service does return JSON I change return to a string and try create a JSONObject but still doesn´t work and return null.
How can I do this ?
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public static JSONObject get(String url){
JSONObject jsonObj = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
HttpEntity entity = httpResponse.getEntity();
if(entity != null){
String s = EntityUtils.toString(entity);
jsonObj = new JSONObject(s);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObj;
}