Closed joshgontijo closed 7 years ago
From #53
I've gone about this several different ways now, and I'm running into roadblocks at each point. Here's the code that calls unirest:
package com.akqa.glass.recipie; import android.util.Log; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; //import org.apache.http.HttpResponse; //import org.shaded.apache.http.HttpHeaders; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; private static final String TAG = JSONParser.class.getSimpleName(); // constructor public JSONParser() { } public JSONObject getCamFindJSON(String type, String input) { Log.d("PARSER", "Inside Parser"); /* * Request processing from API */ if(type == "request"){ try { HttpResponse<JsonNode> response = Unirest.post("https://camfind.p.mashape.com/image_requests") .header("X-Mashape-Key", "Fhn5jZi5ixmshwnJMy7CGyj5yDCnp15DTQZjsniuwpVHfYHvFJ") .field("image_request[image]", new File(input)) .field("image_request[language]", "en") .field("image_request[locale]", "en_US") .asJson(); } catch (UnirestException e) { Log.d(TAG, "Couldn't get request"); e.printStackTrace(); } } /* * Receive response from API */ else if(type == "response"){ try { HttpResponse<JsonNode> response = Unirest.get("https://camfind.p.mashape.com/image_responses/" + input) .header("X-Mashape-Key", "Fhn5jZi5ixmshwnJMy7CGyj5yDCnp15DTQZjsniuwpVHfYHvFJ") .asJson(); } catch (UnirestException e) { Log.d(TAG, "Couldn't get reponse"); e.printStackTrace(); } } /* * Parse Response into readable JSON */ try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "n"); Log.d("Raw Data", line); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); e.printStackTrace(); } catch (Exception e) { Log.e("JSON Parse", "Unknown Error"); e.printStackTrace(); } // return JSON String return jObj; } }
From #53
I've gone about this several different ways now, and I'm running into roadblocks at each point. Here's the code that calls unirest: