I'm trying to create a google glass application that uses the CamFind api to recognize objects from the camera. I've gotten maven to install, but it won't assemble the "master jar", so I'm adding dependencies to the gradle build path. I've gotten it to compile without any errors (granted there are 8 warnings), but it won't complete the call to the api without erroring out.
Here's the relevant code:
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;
// For if I'm using Unirest - don't it's not working...
//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[locale]", "en_US")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
}
/*
* Receive response from API
*/
else if(type == "response"){
// These code snippets use an open-source library. http://unirest.io/java
try {
HttpResponse<JsonNode> response = Unirest.get("https://camfind.p.mashape.com/image_responses/" + input)
.header("X-Mashape-Key", "Fhn5jZi5ixmshwnJMy7CGyj5yDCnp15DTQZjsniuwpVHfYHvFJ")
.asJson();
} catch (UnirestException e) {
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;
}
}
And here's the relevant gradle build path:
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('C:/Users/torti_000/Documents/Recip.ie/libs/unirest-java-1.3.26.jar')
compile 'org.apache.httpcomponents:httpclient:4.3.5'
// compile 'org.apache.httpcomponents:httpclient-android:4.3.3'
compile 'org.apache.httpcomponents:httpmime:4.3.5'
compile 'org.apache.httpcomponents:httpasyncclient:4.0.2'
}
Finally, here's the logcat output (I'm pretty sure it's the first line that's causing all the problems):
11-25 14:21:59.589 6847-7099/com.akqa.glass.recipie E/dalvikvm﹕ Could not find class 'org.apache.http.impl.client.CloseableHttpClient', referenced from method com.mashape.unirest.http.Unirest.shutdown
11-25 14:21:59.589 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ VFY: unable to resolve check-cast 451 (Lorg/apache/http/impl/client/CloseableHttpClient;) in Lcom/mashape/unirest/http/Unirest;
11-25 14:21:59.589 6847-7099/com.akqa.glass.recipie D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x0006
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie I/dalvikvm﹕ Could not find method org.apache.http.client.methods.HttpRequestBase.releaseConnection, referenced from method com.mashape.unirest.http.HttpClientHelper.request
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ VFY: unable to resolve virtual method 1095: Lorg/apache/http/client/methods/HttpRequestBase;.releaseConnection ()V
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0012
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie I/dalvikvm﹕ Could not find method org.apache.http.client.methods.HttpRequestBase.releaseConnection, referenced from method com.mashape.unirest.http.HttpClientHelper.request
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ VFY: unable to resolve virtual method 1095: Lorg/apache/http/client/methods/HttpRequestBase;.releaseConnection ()V
11-25 14:21:59.605 6847-7099/com.akqa.glass.recipie D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0021
11-25 14:21:59.613 6847-7099/com.akqa.glass.recipie I/dalvikvm﹕ Could not find method org.apache.http.client.config.RequestConfig.custom, referenced from method com.mashape.unirest.http.options.Options.refresh
11-25 14:21:59.613 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ VFY: unable to resolve static method 1069: Lorg/apache/http/client/config/RequestConfig;.custom ()Lorg/apache/http/client/config/RequestConfig$Builder;
11-25 14:21:59.613 6847-7099/com.akqa.glass.recipie D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0020
11-25 14:21:59.613 6847-7099/com.akqa.glass.recipie D/dalvikvm﹕ DexOpt: unable to opt direct call 0x070c at 0x49 in Lcom/mashape/unirest/http/options/Options;.refresh
11-25 14:21:59.613 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/mashape/unirest/http/options/Options;
11-25 14:21:59.620 6847-7099/com.akqa.glass.recipie W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x4162bbd8)
11-25 14:21:59.620 6847-7101/com.akqa.glass.recipie D/PARSER﹕ Inside Parser
11-25 14:21:59.620 6847-7101/com.akqa.glass.recipie I/dalvikvm﹕ Rejecting re-init on previously-failed class Lcom/mashape/unirest/http/options/Options; v=0x0
11-25 14:21:59.620 6847-7099/com.akqa.glass.recipie E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.akqa.glass.recipie, PID: 6847
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:314)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.client.config.RequestConfig
at com.mashape.unirest.http.options.Options.refresh(Options.java:45)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:34)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:153)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:133)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at com.akqa.glass.recipie.JSONParser.getCamFindJSON(JSONParser.java:42)
at com.akqa.glass.recipie.RecipIE$CamFindRequest.doInBackground(RecipIE.java:288)
at com.akqa.glass.recipie.RecipIE$CamFindRequest.doInBackground(RecipIE.java:275)
at android.os.AsyncTask$2.call(AsyncTask.java:302)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
11-25 14:21:59.628 6847-7101/com.akqa.glass.recipie W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x4162bbd8)
11-25 14:21:59.628 6847-7101/com.akqa.glass.recipie I/Process﹕ Sending signal. PID: 6847 SIG: 9
The way to solve this is to compile all the dependencies into the mega jar as stated in the instructions. I couldn't do that, so I am using retrofit instead. I'll later update this with a link to a blog post about how to use retrofit with Mashape APIs.