Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public JSONArray GetCityDetails(String StateID) {
- @SuppressWarnings("deprecation")
- String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);
- HttpEntity httpEntity = null;
- try{
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- HttpResponse httpResponse = httpClient.execute(httpGet);
- httpEntity = httpResponse.getEntity();
- } catch(ClientProtocolException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- JSONArray jsonArray = null;
- if(httpEntity !=null){
- try{
- InputStream entityResponse = httpEntity.getContent();
- // not working
- String entityResponseAfterFunctionCall2 = readFully(entityResponse);
- // not working
- String entityResponseAfterFunctionCall3 = letsDoThisAgain(entityResponse);
- Log.e("Entity Response: ", entityResponseAfterFunctionCall3);
- jsonArray = new JSONArray(entityResponseAfterFunctionCall3);
- } catch(JSONException e){
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- return jsonArray;
- }
- public String readFully(InputStream entityResponse) throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int length = 0;
- while ((length = entityResponse.read(buffer)) != -1) {
- baos.write(buffer, 0, length);
- }
- return baos.toString();
- }
- public String letsDoThisAgain(InputStream entityResponse){
- InputStreamReader is = new InputStreamReader(entityResponse);
- StringBuilder sb = new StringBuilder();
- BufferedReader br = new BufferedReader(is);
- try {
- String read = br.readLine();
- while(read !=null){
- sb.append(read);
- read = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement