0

I am trying to call a servlet in Tomcat running on local from android app. I am not sure if it is reaching the servlet, because I don't see any system printout from the servlet. When I hit the url on the browser, I see something, but not from android app.

I have this in my AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Here is my code.

            HttpClient client = new DefaultHttpClient();
        HttpPost method = new HttpPost("http://192.168.1.6:8080/sampleweb/request");

        List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("req", xml));

        method.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = (BasicHttpResponse) client.execute(method);

        InputStream ips = response.getEntity().getContent();
        BufferedReader buf = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new Exception(response.getStatusLine().getReasonPhrase());
        }
        StringBuilder sb = new StringBuilder();
        String s;
        while (true) {
            s = buf.readLine();
            if (s == null || s.length() == 0)
                break;
            sb.append(s);
        }
        buf.close();
        ips.close();

        System.out.println("response="+sb.toString());

        client.getConnectionManager().shutdown();
1
  • Output response.getStatusLine().getStatusCode() to Logcat, see what your got, it tells you the basic info about your httpResponse. Commented Feb 28, 2012 at 8:23

1 Answer 1

1

Are you in Honeycomb, if so... you can no longer do this in the display thread, so, the subsequent question is, are you running this in the display thread?

Also... what does System.out.println do in Android?? I thought you had to throw things out to the Log... is there still a System.out?!

Otherwise, just glancing at it, it looks like it should work.

Also, why not have the servlet print something to a server log, that way you can get a better idea where the breakdown might be.

[edit] try this...

public String makeCall(){
    HttpClient client = new HttpClient();
    PostMethod filePost = new PostMethod(URL_PATH);
    client.setConnectionTimeout(timeout);
    String ret = "";
    try{                        
        if(nvpArray != null)
            filePost.setRequestBody(nvpArray);                   
    }catch(Exception e){
        Log.d(TAG, "upload failed: " + e.toString());
    }              
    try{            
        responseCode = client.executeMethod(filePost);

        Log.d(TAG,"statusCode>>>" + responseCode);
        //Log.d(TAG,"statusLine>>>" + filePost.getResponseBodyAsString());  
        ret = filePost.getResponseBodyAsString();               
    }catch(HttpException e){
        Log.d(TAG, e.toString());           
        return "YSERROR:" + e.toString();
    }catch(IOException e){          
        Log.d(TAG, e.toString());
        return "YSERROR:" + e.toString();
    }        
    catch(Exception e){                 
        Log.d(TAG, e.toString());
        return "YSERROR:" + e.toString();
    };        

    filePost.releaseConnection();   
    return ret;

}
Sign up to request clarification or add additional context in comments.

6 Comments

I am running it on android 2.2 for target. For system.out, I forgot commenting it out, because I copied standalone java client to android class. And I don't see any log on tomcat. It doesn't look like that it reaches the servlet.
well, the key is finding out why it's not making the call. Have you tried calling the servlet through the device's webkit browser? (or are you using your computer browser to test?). I would attach the debugger and see what your HttpClient tells you as you step through its attempt to reach your servlet.
I used the browser on the android. It can reach the servlet.
I am getting "Connection refused" for http://<host>:8080/. I already set the "android.permission.INTERNET". Is there any other setting or permission that I need to set?
no... that's very strange, if you're sure that you have the host set correctly then the only thing left is to figure out what your headers contain, since they must be missing something that your servlet wants to see, and is refusing the connection without (since the servlet is accepting a connection from your tablet's browser).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.