-
-
Save anonymous/b0a261d6c88fafe5afeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.ebeletskiy; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
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 org.json.JSONException; | |
import org.json.JSONObject; | |
import android.util.Log; | |
public class HttpManager { | |
private HttpClient httpClient; | |
private HttpGet httpGet; | |
private HttpResponse httpResponse; | |
private HttpEntity httpEntity; | |
private InputStream instream; | |
private BufferedReader buffReader; | |
private String readLine; | |
private StringBuilder stringBuilder; | |
private JSONObject json; | |
HttpManager() { | |
httpClient = new DefaultHttpClient(); | |
httpGet = new HttpGet(MyConfig.WEB_SERVER); | |
if (MyConfig.DEBUG) Log.d("Dev", "HttpManager created"); | |
} | |
/*Makes a GET Request to specified URL | |
Return simple JSON*/ | |
public JSONObject getRequest(URI url) { | |
if (MyConfig.DEBUG) Log.d("Dev", "HttpManager's getRequest() invoked"); | |
try { | |
httpResponse = httpClient.execute(httpGet); | |
if (httpResponse.getStatusLine().getStatusCode() == 200) { | |
httpEntity = httpResponse.getEntity(); | |
if (httpEntity != null) { | |
instream = httpEntity.getContent(); | |
String convertedString = convertStreamToString(instream); | |
return convertToJSON(convertedString); | |
} | |
} | |
return null; | |
} catch (ClientProtocolException e) { | |
if (MyConfig.DEBUG) Log.d("Dev", "Something went wrong in getRequest()"); | |
e.printStackTrace(); | |
return null; | |
} catch (IOException e) { | |
if (MyConfig.DEBUG) Log.d("Dev", "Something went wrong in getRequest()"); | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
/*Reads data from InputStream and put it in String*/ | |
private String convertStreamToString(InputStream instream) { | |
buffReader = new BufferedReader(new InputStreamReader(instream)); | |
stringBuilder = new StringBuilder(); | |
try { | |
while ((readLine = buffReader.readLine()) != null) { | |
stringBuilder.append(readLine + "\n"); | |
if (MyConfig.DEBUG) Log.d("Dev", "Read response " + readLine); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
instream.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
return stringBuilder.toString(); | |
} | |
/*Converts String to JSON*/ | |
private JSONObject convertToJSON(String string) { | |
try { | |
return json = new JSONObject(string); | |
} catch (JSONException e) { | |
if (MyConfig.DEBUG) Log.d("Dev", "Converting String to JSON was't successfull"); | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment