3

I have an HTTP request that throws a null exception.

Here's the code:

public String updateRounds() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://technet.rapaport.com/HTTP/Prices/CSV2_Round.aspx");

    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("Username", _user));
    nameValuePairs.add(new BasicNameValuePair("Password", _password));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = "";
    // Execute HTTP Post Request
    try {
        response = httpclient.execute(httppost, responseHandler); // it gets
                                                                  // exception
                                                                  // here
    } catch (Exception e) {
        e.getMessage(); // e is null
    }

    return response;
}

Anyone has any idea on why this happening?

Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46
orelzion
  • 2,452
  • 4
  • 30
  • 46
  • Has your application got the UsesInternet permission? Also try catching the other types of exception that the httpclient can throw instead of the generic type – Dazzy_G Jun 12 '12 at 14:00

2 Answers2

4

Thank you all for trying to help. I've changed the

e.getMessage();

to

e.printStackTrace();

And I saw that the exception was about ssl certificate. So I handled that and now it works. Thank you all.

orelzion
  • 2,452
  • 4
  • 30
  • 46
2

You have problem in following.

String response = "";
    // Execute HTTP Post Request
    try {           
        response = httpclient.execute(httppost, responseHandler); //it gets exception here

What is responseHandler?

httpclient.execute returns HttpResponse and you are trying to return it in response (which is String).

So Change

  String response 

to

HttpResponse response

If you really want to return string then add following code.

    bufferedReader = new BufferedReader(
               new InputStreamReader(response.getEntity().getContent()));
       StringBuffer stringBuffer = new StringBuffer("");
       String line = "";
       String LineSeparator = System.getProperty("line.separator");
       while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line + LineSeparator); 
       }

      bufferedReader.close();  
      return stringBuffer.toString();
Vipul
  • 27,808
  • 7
  • 60
  • 75