I'm developing for Google Glass and have been having issues uploading files from Glass to a servlet application I am using to manage API calls. I set up a client ID and secret to authenticate the api calls from the servlet app, using the Developer's console. I've seen these links mentioning http requests from glass:
HTTP Requests in Glass GDK 1
Google Glass upload files using http-client
...but they have not been incredibly helpful.
I do have the Internet permissions set up in the manifest file. This is the code I have:
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = "MyAsyncTask";
private static String FILE_PATH;
private String fileType;
private static final String HOST_URL="http://10.0.2.2:8080/main";
private static java.io.File fileToUpload;
public MyAsyncTask(String filePath){
FILE_PATH=filePath;
fileToUpload=new java.io.File(FILE_PATH);
Log.d(TAG,FILE_PATH);
fileType=FILE_PATH.substring(FILE_PATH.indexOf("."));
}
@Override
protected Void doInBackground(Void... params) {
post();
return null;
}
private int post() {
int resultCode = 0;
String url = HOST_URL;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("source", "glass"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
resultCode = response.getStatusLine().getStatusCode();
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (Exception e) {
Log.e("POST", e.getMessage());
}
return resultCode;
}
The result of calling MyAsyncTask(filePath).execute(); is that the connection is refused. I set the URL to what it is because I read that localhost might be referring to the Glass device like it does when the Android emulator is run, but the servlet is running at http://localhost:8080.
For reference I use Jetty 6.1.x for my servlet.
What could my issue be? Does Glass not support firing HTTP requests? Should I be trying to authenticate where I have not? I would be happy to provide any other referential information if requested. I'd very much appreciate any guidance and expertise. Thank you.