package com.zhoujh.appstore;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public class JSONProvider {
public static String getJSONData(String url) throws ClientProtocolException, IOException{
String result="";
HttpGet httpGet=new HttpGet(url);
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse=null;
try {
httpResponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
if (httpEntity!=null) {
InputStream inputStream=httpEntity.getContent();
result=convertStreamToString(inputStream);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
throw e;
} finally {
httpClient.getConnectionManager().shutdown();
httpResponse = null;
}
return result;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(is),512 * 1024);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),
e);
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}