1

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.

Community
  • 1
  • 1
cheft
  • 153
  • 2
  • 10
  • Google Glass is like a regular Android device. So http request behave the same. Working in a local network is not related to Google but any device. You should look for how to access local network from Android device. You'll find your answer – Simon Marquis Jan 22 '15 at 19:20
  • @SimonMarquis. I've looked into this extensively. I know that the localhost of the running device is the device itself. I tried the network IP address but still I get the connection refused exception – cheft Jan 22 '15 at 20:46
  • Did you try to use the private local IP address of the machine running the server? Edit: Take a look at this for instance: http://stackoverflow.com/questions/19132059/how-to-allow-remote-access-to-my-wamp-server-for-mobileandroid – Simon Marquis Jan 22 '15 at 20:49
  • I work on a mac and I used ifconfig to get this address and used it. It still gave me a connection refused message. I have not altered the httpd.conf file mentioned however – cheft Jan 22 '15 at 21:37
  • If you don't, the server is going to refuse the device. – Simon Marquis Jan 22 '15 at 21:38
  • Your httpd.conf file must not deny your Glass device, at the very least. – Koh Jan 23 '15 at 00:09
  • Is the httpd.conf file related to a firewall? I got a connection refused because of a timeout and I read [here](http://stackoverflow.com/questions/23705592/android-socket-timeout-while-connecting) that it might be due to a firewall. In either case, how might I resolve the possibility of a firewall? And how might I know whether a firewall is indeed the issue? – cheft Jan 23 '15 at 17:32

2 Answers2

0

Glass supports HTTP calls - I don't think that is your problem.

The issue seems to more likely lie in your servlet or network configuration (or some combination of them).

Some things to check:

  • Make sure your Glass is connected to your local network, and the same local network that your server is connected to. The 10.x.x.x address for your server indicates that it is on a private network, and you need to make sure Glass is on the same private network.
    • You can check and confirm which wifi network it is connected to by going to the settings screen on Glass. Make sure it says "Wi-Fi". Tapping on the screen, it should tell you what wifi network you're attached to.
    • To verify, you may want to get the IP address of the device. See this answer for some code which may assist you in doing so.
  • Make sure you can get to the server at the address you've given. Use a browser on another machine (on the same network) if you can. Make sure you use the exact URL the Glass app will be using - http://10.0.2.2:8080/main
    • Using the IP address, rather than "localhost" is important.
    • Testing it from the same machine vs another machine on the same network might point to a firewall on the machine blocking the port or not.
  • Confirm what port your servlet is listening to.
    • You don't talk about what servlet engine you're using, so it is difficult to point you at exactly which file you should be checking for the configuration. If you let us know, we might be able to point you in the right direction if you don't know.
    • The URL on Glass is looking to 8080, so make sure your servlet is also set to use that port. (8080 is a pretty typical one, but not guaranteed.) Your servlet should not open a port on its own - let the servlet container handle that.
  • Check your servlet logs, particularly the access log, to see if the Glass device, and your manual test above, are actually connecting to it. Or if anything at all can connect to it.
Community
  • 1
  • 1
Prisoner
  • 49,922
  • 7
  • 53
  • 105
0

I figured out my issue and I realized I could have provided some more information. My apologies. In any case, the Jetty server was only running on localhost (127.0.0.1). To fix this, I edited the pom.xml file of my maven project as follows:

-Added to the appengine plugin tag under the configuration tag an address tag with address 0.0.0.0 and a port tag with port 8080. This allowed it to run on localhost.
-Then appengine gave a 400 error when I tried to access the raw local ip so I edited the hosts file on my machine located at something like /private/etc/hosts and added this line:

my-wlan-ip fakedomain.com

But I also had to add fakedomain.com:8080/oauth2callback to my registered redirect uris in the Google Developer's Console under Credentials.

Which let me access this my devserver from other devices.

cheft
  • 153
  • 2
  • 10