Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javmedapp.Master;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.ProtocolException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- 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 HttpManager {
- HttpClient httpClient = new DefaultHttpClient();
- public String OpenHttpConnection(String urlString) throws IOException,
- ProtocolException {
- URI uri = null;
- try {
- uri = new URI(urlString.replace(" ", "%20"));
- } catch (URISyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String jsonString = null;
- HttpResponse httpResponse;
- // urlString = String.format(urlString);
- // Log.v("Encoded URL Request",urlString);
- HttpGet get = new HttpGet(uri);
- System.out.println("after replacing~~~~~~~~" + uri);
- httpResponse = httpClient.execute(get);
- Log.d("", httpResponse.getStatusLine().toString());
- HttpEntity entity = httpResponse.getEntity();
- if (entity != null) {
- InputStream inputStream = entity.getContent();
- jsonString = convertStreamToString(inputStream);
- inputStream.close();
- System.out.println(jsonString.toString());
- Log.v("Json Response", jsonString + jsonString.length());
- }
- return jsonString;
- }
- private String convertStreamToString(InputStream is) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is),
- 8192);
- StringBuilder sb = new StringBuilder();
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- } catch (IOException e) {
- Log.e("Convert Input Stream",
- "Caught IOException in convertStreamToString()", e);
- return null;
- } finally {
- try {
- is.close();
- } catch (IOException e) {
- Log.e("Convert Input Stream",
- "Caught IOException in convertStreamToString()", e);
- return null;
- }
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement