Skip to content

Instantly share code, notes, and snippets.

@erikeldridge
Last active December 10, 2015 07:58
Show Gist options
  • Save erikeldridge/4404258 to your computer and use it in GitHub Desktop.
Save erikeldridge/4404258 to your computer and use it in GitHub Desktop.
android utility functions
package com.example.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONArray;
import android.util.Log;
public class Util {
public static String readStream(InputStream in){
Log.v("Util", "readStream");
BufferedReader reader = null;
String json = "";
try {
String line = "";
reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null){
json += line;
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
return json;
}
public static JSONArray parseJson(String json){
Log.v("Util", "parseJson");
JSONArray data = null;
try{
data = new JSONArray(json);
}catch(Exception e){
e.printStackTrace();
}
if(null == data){
try{
data = new JSONArray("[]");
}catch(Exception e){
e.printStackTrace();
}
}
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment