[android-developers] query about the usage of HttpsURLConnection

2010-03-09 Thread Hugo Wang
Hi all,

I am using HttpsURLConnection to post data to a web server.

Here is the function I wrote to get the result.  (see the code below)

Very strangely, when the function be first called in my program it
would very likely return -1; and then I re-call the function after the
first call, it would always work ok.

So I wonder if there is anything I am missing in the code that when
the function be firstly called, the missing party is be initialized,
in that the followed call goes OK.

Could anyone give some hints on this?

Here is my code of the function:
=

Public MyReturn executeHttpsGet(Context context, String url) {

   
   try {
URL u = new URL(url);
HttpsURLConnection httpConnection;
httpConnection = (HttpsURLConnection)u.openConnection();


  httpConnection.setSSLSocketFactory(mSslContext.getSocketFactory());
httpConnection.setHostnameVerifier(mVerifier);
httpConnection.setDoInput(true);
httpConnection.setConnectTimeout(DEFAULTTIMEOUT);
httpConnection.setUseCaches(true);

 httpConnection.connect();
ret.code = httpConnection.getResponseCode();
Log.i(TAG, ResponseCode: +String.valueOf(ret.code));
 BufferedReader reader;
if (ret.code == 200) {
reader = new BufferedReader(new
InputStreamReader(httpConnection.getInputStream()));
} else {
reader = new BufferedReader(new
InputStreamReader(httpConnection.getErrorStream()));
}

String line;
ret.body = ;
while((line = reader.readLine())!= null) {
ret.body = ret.body + line;
}
ret.reader = new StringReader(ret.body);
PsLogUtil.appendMessage(context, TAG, Responsebody: +ret.body);
 httpConnection.disconnect();
}catch (Exception e) {
e.printStackTrace();
Log.e(TAG, +e.getMessage());
ret.body = ;
ret.code = -1;
}


return ret;
}



Thanks very much for your kindly help.

Best

Hugo

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

RE: [android-developers] query about the usage of HttpsURLConnection

2010-03-09 Thread Trung-Liem Vo
Hill Hugo, 

 

Nothing miss in your code. 

I had the same problem with you before when I got data  from the server
using HttpsURLConnection. And I try connect again that would be fine. 

 

If you find the reason, would you tell me? 

 

Thanks,

Vu-Nguyen

 

From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of Hugo Wang
Sent: Wednesday, March 10, 2010 10:26 AM
To: android-developers@googlegroups.com
Subject: [android-developers] query about the usage of HttpsURLConnection

 

Hi all,

 

I am using HttpsURLConnection to post data to a web server.  

 

Here is the function I wrote to get the result.  (see the code below)

 

Very strangely, when the function be first called in my program it would
very likely return -1; and then I re-call the function after the first call,
it would always work ok.

 

So I wonder if there is anything I am missing in the code that when the
function be firstly called, the missing party is be initialized, in that
the followed call goes OK. 

 

Could anyone give some hints on this? 

 

Here is my code of the function:

=

 

Public MyReturn executeHttpsGet(Context context, String url) {

  

   

   try {

URL u = new URL(url);

HttpsURLConnection httpConnection;

httpConnection =
(HttpsURLConnection)u.openConnection();

 

 
httpConnection.setSSLSocketFactory(mSslContext.getSocketFactory());

 
httpConnection.setHostnameVerifier(mVerifier);

httpConnection.setDoInput(true);

 
httpConnection.setConnectTimeout(DEFAULTTIMEOUT);

httpConnection.setUseCaches(true);

 



httpConnection.connect();

ret.code =
httpConnection.getResponseCode();

Log.i(TAG, ResponseCode:
+String.valueOf(ret.code));



BufferedReader reader;

if (ret.code == 200) {

reader = new
BufferedReader(new InputStreamReader(httpConnection.getInputStream()));

} else {

reader = new
BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));

}

 

String line;

ret.body = ;

while((line = reader.readLine())!= null)
{

ret.body = ret.body + line;

}

ret.reader = new StringReader(ret.body);


PsLogUtil.appendMessage(context, TAG,
Responsebody: +ret.body);



httpConnection.disconnect();

}catch (Exception e) {

e.printStackTrace();

Log.e(TAG, +e.getMessage());

ret.body = ;

ret.code = -1;

}  

 

 

return ret;

}

 



 

Thanks very much for your kindly help.

 

Best

 

Hugo

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] query about the usage of HttpsURLConnection

2010-03-09 Thread Hugo Wang
Thanks for Vu-Nguyen's reply.   I did more test, and here is some output
about the err:

=
W/System.err( 1390): java.lang.NullPointerException
W/System.err( 1390):  at java.io.Reader.init(Reader.java:72)
W/System.err( 1390):  at
java.io.InputStreamReader.init(InputStreamReader.java:72)
W/System.err( 1390):  at
android.test.MyTestHttpRequest.executeHttpsPost(MyTestHttpRequest.java:262)
W/System.err( 1390):  at
android.test.MyTestServerToolkitL.loginUser(MyTestUserServerToolkitL.java:40)
W/System.err( 1390):  at
com.me.mytest.MyTestLoginActivity$6.run(PsLoginActivity.java:279)
=

It seems some null pointer is the reason?  Is it?

Best
Hugo

2010/3/10 Trung-Liem Vo lie...@gmail.com

  Hill Hugo,



 Nothing miss in your code.

 I had the same problem with you before when I got data  from the server
 using HttpsURLConnection. And I try connect again that would be fine.



 If you find the reason, would you tell me?



 Thanks,

 Vu-Nguyen



 *From:* android-developers@googlegroups.com [mailto:
 android-develop...@googlegroups.com] *On Behalf Of *Hugo Wang
 *Sent:* Wednesday, March 10, 2010 10:26 AM
 *To:* android-developers@googlegroups.com
 *Subject:* [android-developers] query about the usage of
 HttpsURLConnection



 Hi all,



 I am using HttpsURLConnection to post data to a web server.



 Here is the function I wrote to get the result.  (see the code below)




 Very strangely, when the function be first called in my program it would very 
 likely return -1; and then I re-call the function after the first call, it 
 would always work ok.




 So I wonder if there is anything I am missing in the code that when the 
 function be firstly called, the missing party is be initialized, in that 
 the followed call goes OK.



 Could anyone give some hints on this?



 Here is my code of the function:

 =



 Public MyReturn executeHttpsGet(Context context, String url) {





try {

 URL u = new URL(url);

 HttpsURLConnection httpConnection;

 httpConnection =
 (HttpsURLConnection)u.openConnection();




   httpConnection.setSSLSocketFactory(mSslContext.getSocketFactory());


 httpConnection.setHostnameVerifier(mVerifier);

 httpConnection.setDoInput(true);


 httpConnection.setConnectTimeout(DEFAULTTIMEOUT);

 httpConnection.setUseCaches(true);





 httpConnection.connect();

 ret.code =
 httpConnection.getResponseCode();

 Log.i(TAG, ResponseCode:
 +String.valueOf(ret.code));



 BufferedReader reader;

 if (ret.code == 200) {

 reader = new
 BufferedReader(new InputStreamReader(httpConnection.getInputStream()));

 } else {

 reader = new
 BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));

 }



 String line;

 ret.body = ;

 while((line = reader.readLine())!=
 null) {

 ret.body = ret.body +
 line;

 }

 ret.reader = new
 StringReader(ret.body);

 PsLogUtil.appendMessage(context, TAG,
 Responsebody: +ret.body);



 httpConnection.disconnect();

 }catch (Exception e) {

 e.printStackTrace();

 Log.e(TAG, +e.getMessage());

 ret.body = ;

 ret.code = -1;

 }





 return ret;

 }



 



 Thanks very much for your kindly help.



 Best



 Hugo

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.comandroid-developers%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit