user826323
user826323

Reputation: 2338

android - using apache HttpClient

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();

Upvotes: 0

Views: 687

Answers (1)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

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;

}

Upvotes: 1

Related Questions