<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Commentaires sur : Different ways to access HTTP resources from Android</title>
	<atom:link href="http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/</link>
	<description>Open Source Software, Java, Android, Continuous Integration</description>
	<lastBuildDate>Fri, 20 Jan 2012 00:18:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>Par : Arcao</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31621</link>
		<dc:creator>Arcao</dc:creator>
		<pubDate>Wed, 11 Jan 2012 18:01:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31621</guid>
		<description>I think the best way how to get String content from InputStrem is:

&lt;pre lang=&quot;java&quot; line=&quot;1&quot;&gt;
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream, &quot;UTF-8&quot;));

StringBuilder sb=new StringBuilder();

char[] buffer = new char[8192];
int len;

while ((len = br.read(buffer)!= -1) {
  sb.append(buffer, 0, len);
}

String content = sb.toString();
&lt;/pre&gt;

Note: Don&#039;t forget on right charset, see second parameter in InputStreamReader constructor.</description>
		<content:encoded><![CDATA[<p>I think the best way how to get String content from InputStrem is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">BufferedReader</span> br<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>inputStream, <span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
StringBuilder sb<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> len<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>len <span style="color: #339933;">=</span> br.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003399;">String</span> content <span style="color: #339933;">=</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Note: Don&#8217;t forget on right charset, see second parameter in InputStreamReader constructor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : pratik</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31525</link>
		<dc:creator>pratik</dc:creator>
		<pubDate>Tue, 15 Nov 2011 10:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31525</guid>
		<description>hi Andrea
i have faced such problem of unknown host .
check your internet connection ..
or if you are testing with emulator so set dns server for emulator like

start cmd
change directory to android-sdk/tools 
and 
fire this command

emulator.exe -avd [your avd name] -dns-server 192.168.1.1

thats it. 

run your app...

hope this will help you</description>
		<content:encoded><![CDATA[<p>hi Andrea<br />
i have faced such problem of unknown host .<br />
check your internet connection ..<br />
or if you are testing with emulator so set dns server for emulator like</p>
<p>start cmd<br />
change directory to android-sdk/tools<br />
and<br />
fire this command</p>
<p>emulator.exe -avd [your avd name] -dns-server 192.168.1.1</p>
<p>thats it. </p>
<p>run your app&#8230;</p>
<p>hope this will help you</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : diyism</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31449</link>
		<dc:creator>diyism</dc:creator>
		<pubDate>Thu, 04 Aug 2011 07:20:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31449</guid>
		<description>&lt;code&gt;
ByteArrayOutputStream stream_bytes=new ByteArrayOutputStream();
int cnt=0;
byte[] buffer=new byte[512];
while ((cnt=stream_content.read(buffer))!=-1)
      {stream_bytes.write(buffer, 0, cnt);
      }
String response_text=new String(stream_bytes.toByteArray());
&lt;/code&gt;
or
&lt;code&gt;
BufferedReader br=new BufferedReader(new InputStreamReader(stream_content), 4096);
String line;
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null)
      {sb.append(line);
      }
String contentOfMyInputStream=sb.toString();
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
ByteArrayOutputStream stream_bytes=new ByteArrayOutputStream();<br />
int cnt=0;<br />
byte[] buffer=new byte[512];<br />
while ((cnt=stream_content.read(buffer))!=-1)<br />
      {stream_bytes.write(buffer, 0, cnt);<br />
      }<br />
String response_text=new String(stream_bytes.toByteArray());<br />
</code><br />
or<br />
<code><br />
BufferedReader br=new BufferedReader(new InputStreamReader(stream_content), 4096);<br />
String line;<br />
StringBuilder sb=new StringBuilder();<br />
while ((line=br.readLine())!=null)<br />
      {sb.append(line);<br />
      }<br />
String contentOfMyInputStream=sb.toString();<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : diyism</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31448</link>
		<dc:creator>diyism</dc:creator>
		<pubDate>Thu, 04 Aug 2011 05:09:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31448</guid>
		<description>Maybe you could use convertStreamToString to convert the InputStream into a string:

&lt;code&gt;
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Maybe you could use convertStreamToString to convert the InputStream into a string:</p>
<p><code><br />
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Sam</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31432</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Fri, 15 Jul 2011 23:15:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31432</guid>
		<description>Hi Anthony,

You may find this somehow unrelated to your efforts towards accessing http resources from Android but I will appreciate it if you can provide me with your thoughts regarding to my challenge. 

I&#039;m trying to capture some statistics like number of 302&#039;s and 200 Oks or the time it takes for the client to receive the content from the streamer (time it takes from the actual request (URL) to the http client till http client sends the content back to the media player). The cleanest way to do this is to extend HttpClient code. The challenge for me is to know that if the media player (any kind) on Android will use my extended version of HttpClient or not. I will appreciate it if you can help me at least based on your experience what you think about this. 

Cheers, STZ</description>
		<content:encoded><![CDATA[<p>Hi Anthony,</p>
<p>You may find this somehow unrelated to your efforts towards accessing http resources from Android but I will appreciate it if you can provide me with your thoughts regarding to my challenge. </p>
<p>I&#8217;m trying to capture some statistics like number of 302&#8242;s and 200 Oks or the time it takes for the client to receive the content from the streamer (time it takes from the actual request (URL) to the http client till http client sends the content back to the media player). The cleanest way to do this is to extend HttpClient code. The challenge for me is to know that if the media player (any kind) on Android will use my extended version of HttpClient or not. I will appreciate it if you can help me at least based on your experience what you think about this. </p>
<p>Cheers, STZ</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : vijay</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31421</link>
		<dc:creator>vijay</dc:creator>
		<pubDate>Tue, 21 Jun 2011 11:09:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31421</guid>
		<description>Reading / Sending a cookie along with the requests.can you provide entire code for this i was struggle with this lot. please.</description>
		<content:encoded><![CDATA[<p>Reading / Sending a cookie along with the requests.can you provide entire code for this i was struggle with this lot. please.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Andrea</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-2/#comment-31417</link>
		<dc:creator>Andrea</dc:creator>
		<pubDate>Wed, 08 Jun 2011 15:31:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31417</guid>
		<description>Hi Anthony,
I tryed both methods to get http resources described above, very similar to other examples I found on the web, but I can&#039;t manage them to work.
I always get the following exception:

INFO/System.out(278): Excpetion = java.net.UnknownHostException: oxavi.com

It seems it&#039;s the same problem described by Bipin Vayalu but I can&#039;t see any reply on this.
I added to my AndroidManifest file the user permission:

   android:name=&quot;android.permission.INTERNET&quot;

but it didn&#039;t solve the problem.
I tryed both on my HW(HTC WildFire android 2.2.1) and on the AVDs with no results.
Do I miss some important configuration for the internet access ?

Do you think it is possible to handle the http connection directly into the event handler or I need to create a separate thread ?

===============  This is the code =====================
final Button buttonSearch = (Button) findViewById(R.id.search);         
buttonSearch.setOnTouchListener(new View.OnTouchListener() {
@Override 
public boolean onTouch(View v, MotionEvent event) 
{
  if(event.getAction() == android.view.MotionEvent.ACTION_DOWN){
  try {
    System.out.println(&quot;\n\n BUTTON PUSHED\n\n&quot;);
    InputStream content = null;
    HttpGet httpGet = new HttpGet(&quot;http://oxavi.com/price.php&quot;);
    HttpClient httpclient = new DefaultHttpClient();
    // Execute HTTP Get Request
    HttpResponse response = httpclient.execute(httpGet);
    content = response.getEntity().getContent();	
    BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);
    String line;
    StringBuilder sb =  new StringBuilder();
    while ((line = rd.readLine()) != null) 
    { sb.append(line);}
    rd.close();
    String contentOfMyInputStream = sb.toString();
    System.out.println(contentOfMyInputStream);
       			
   } catch (Exception e){
       System.out.println(&quot;Excpetion = &quot; + e);}
  }
  return true;  
 }
}); 
===================================================
Many thanks in advance.
   Andrea</description>
		<content:encoded><![CDATA[<p>Hi Anthony,<br />
I tryed both methods to get http resources described above, very similar to other examples I found on the web, but I can&#8217;t manage them to work.<br />
I always get the following exception:</p>
<p>INFO/System.out(278): Excpetion = java.net.UnknownHostException: oxavi.com</p>
<p>It seems it&#8217;s the same problem described by Bipin Vayalu but I can&#8217;t see any reply on this.<br />
I added to my AndroidManifest file the user permission:</p>
<p>   android:name=&nbsp;&raquo;android.permission.INTERNET&nbsp;&raquo;</p>
<p>but it didn&#8217;t solve the problem.<br />
I tryed both on my HW(HTC WildFire android 2.2.1) and on the AVDs with no results.<br />
Do I miss some important configuration for the internet access ?</p>
<p>Do you think it is possible to handle the http connection directly into the event handler or I need to create a separate thread ?</p>
<p>===============  This is the code =====================<br />
final Button buttonSearch = (Button) findViewById(R.id.search);<br />
buttonSearch.setOnTouchListener(new View.OnTouchListener() {<br />
@Override<br />
public boolean onTouch(View v, MotionEvent event)<br />
{<br />
  if(event.getAction() == android.view.MotionEvent.ACTION_DOWN){<br />
  try {<br />
    System.out.println(&laquo;&nbsp;\n\n BUTTON PUSHED\n\n&nbsp;&raquo;);<br />
    InputStream content = null;<br />
    HttpGet httpGet = new HttpGet(&laquo;&nbsp;http://oxavi.com/price.php&nbsp;&raquo;);<br />
    HttpClient httpclient = new DefaultHttpClient();<br />
    // Execute HTTP Get Request<br />
    HttpResponse response = httpclient.execute(httpGet);<br />
    content = response.getEntity().getContent();<br />
    BufferedReader rd = new BufferedReader(new InputStreamReader(content), 4096);<br />
    String line;<br />
    StringBuilder sb =  new StringBuilder();<br />
    while ((line = rd.readLine()) != null)<br />
    { sb.append(line);}<br />
    rd.close();<br />
    String contentOfMyInputStream = sb.toString();<br />
    System.out.println(contentOfMyInputStream);</p>
<p>   } catch (Exception e){<br />
       System.out.println(&laquo;&nbsp;Excpetion = &nbsp;&raquo; + e);}<br />
  }<br />
  return true;<br />
 }<br />
});<br />
===================================================<br />
Many thanks in advance.<br />
   Andrea</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Http request incomplete response &#124; Android JB</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31412</link>
		<dc:creator>Http request incomplete response &#124; Android JB</dc:creator>
		<pubDate>Thu, 19 May 2011 00:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31412</guid>
		<description>[...] and I still can&#8217;t get a valid response. In this blogpost http://blog.dahanne.net/2009/08/16/h&#8230;-from-android/ the guy shows a few ways of doing an Http request I tried the 1st one and the final string I get [...]</description>
		<content:encoded><![CDATA[<p>[...] and I still can&#8217;t get a valid response. In this blogpost <a href="http://blog.dahanne.net/2009/08/16/h&#8230;-from-android/" rel="nofollow">http://blog.dahanne.net/2009/08/16/h&#8230;-from-android/</a> the guy shows a few ways of doing an Http request I tried the 1st one and the final string I get [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : frank</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31411</link>
		<dc:creator>frank</dc:creator>
		<pubDate>Mon, 16 May 2011 19:06:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31411</guid>
		<description>I am trying to display a website from within an android app. I basically want to leave my app and bring up a website on the phones default browser. For example, create a button that says NASDAQ in an app and when it is pressed it loads the default phone browser with the www.nasdaq.com website in it. 

Any help would be greatly appreciated. 
Here is my current code. 


.setNeutralButton(&quot;Buy stock&quot;, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpGet httpget = new HttpGet(&quot;http://www.nasdaq.com/&quot;);
                     HttpResponse response = null;
                        try {
                              response = httpclient.execute(httpget);
                        } catch (ClientProtocolException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                        }
                     HttpEntity entity = response.getEntity();
                     if (entity != null) {
                         InputStream instream = null;
                              try {
                                    instream = entity.getContent();
                              } catch (IllegalStateException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                              } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                              }
                         int l;
                         byte[] tmp = new byte[2048];
                         try {
                                    while ((l = instream.read(tmp)) != -1) {
                                       }
                              } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                              }
                     }

                     
                     
                 }
             })</description>
		<content:encoded><![CDATA[<p>I am trying to display a website from within an android app. I basically want to leave my app and bring up a website on the phones default browser. For example, create a button that says NASDAQ in an app and when it is pressed it loads the default phone browser with the <a href="http://www.nasdaq.com" rel="nofollow">http://www.nasdaq.com</a> website in it. </p>
<p>Any help would be greatly appreciated.<br />
Here is my current code. </p>
<p>.setNeutralButton(&laquo;&nbsp;Buy stock&nbsp;&raquo;, new DialogInterface.OnClickListener() {<br />
                 public void onClick(DialogInterface dialog, int id) {</p>
<p>                     HttpClient httpclient = new DefaultHttpClient();<br />
                     HttpGet httpget = new HttpGet(&laquo;&nbsp;http://www.nasdaq.com/&nbsp;&raquo;);<br />
                     HttpResponse response = null;<br />
                        try {<br />
                              response = httpclient.execute(httpget);<br />
                        } catch (ClientProtocolException e) {<br />
                              // TODO Auto-generated catch block<br />
                              e.printStackTrace();<br />
                        } catch (IOException e) {<br />
                              // TODO Auto-generated catch block<br />
                              e.printStackTrace();<br />
                        }<br />
                     HttpEntity entity = response.getEntity();<br />
                     if (entity != null) {<br />
                         InputStream instream = null;<br />
                              try {<br />
                                    instream = entity.getContent();<br />
                              } catch (IllegalStateException e1) {<br />
                                    // TODO Auto-generated catch block<br />
                                    e1.printStackTrace();<br />
                              } catch (IOException e1) {<br />
                                    // TODO Auto-generated catch block<br />
                                    e1.printStackTrace();<br />
                              }<br />
                         int l;<br />
                         byte[] tmp = new byte[2048];<br />
                         try {<br />
                                    while ((l = instream.read(tmp)) != -1) {<br />
                                       }<br />
                              } catch (IOException e) {<br />
                                    // TODO Auto-generated catch block<br />
                                    e.printStackTrace();<br />
                              }<br />
                     }</p>
<p>                 }<br />
             })</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Rahul Nair</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31410</link>
		<dc:creator>Rahul Nair</dc:creator>
		<pubDate>Thu, 12 May 2011 08:28:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31410</guid>
		<description>@ Upschool : try  

response.getEntity().getContent();   (this will return you InputStream to parse)

instead of just    

 response.getEntity()</description>
		<content:encoded><![CDATA[<p>@ Upschool : try  </p>
<p>response.getEntity().getContent();   (this will return you InputStream to parse)</p>
<p>instead of just    </p>
<p> response.getEntity()</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Sujay</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31409</link>
		<dc:creator>Sujay</dc:creator>
		<pubDate>Mon, 09 May 2011 12:28:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31409</guid>
		<description>Hello,

 I am new to android development can you show how the request and response works in android using any small example(if 1 text field requesting something and taking response when clicked on to button).
can you help me on this. Webservice is new to me...</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p> I am new to android development can you show how the request and response works in android using any small example(if 1 text field requesting something and taking response when clicked on to button).<br />
can you help me on this. Webservice is new to me&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Bipin Vayalu</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31368</link>
		<dc:creator>Bipin Vayalu</dc:creator>
		<pubDate>Fri, 22 Apr 2011 12:05:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31368</guid>
		<description>Hi,

This is a very useful article for beginner like me.

Thanks

But sometime i got the &lt;b&gt;IOException&lt;/b&gt; when call goes to execute method of &lt;b&gt;HttpGet/HttpPost.&lt;/b&gt;

i think this url problem but when i paste same url into web browser it gives me response.

i don&#039;t know why this is done. 

Please, can you provide me the appropriate answer?

Thanks in advance.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>This is a very useful article for beginner like me.</p>
<p>Thanks</p>
<p>But sometime i got the <b>IOException</b> when call goes to execute method of <b>HttpGet/HttpPost.</b></p>
<p>i think this url problem but when i paste same url into web browser it gives me response.</p>
<p>i don&#8217;t know why this is done. </p>
<p>Please, can you provide me the appropriate answer?</p>
<p>Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Upschool</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31361</link>
		<dc:creator>Upschool</dc:creator>
		<pubDate>Tue, 19 Apr 2011 10:03:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31361</guid>
		<description>Hi Anthony,
I am having trouble reading a plain xml file(REST web service).

I have an GET(see code below) Method here(Getting the right result: HTTP status + xml).
However when trying with an POST Method i only get the HTTP status, but the xml file are not displayed at the screen.

Can you help me figure out what am I doing wrong on the POST part.

Notice! I have two service one that requires GET and the other POST
// This is the GET method part
private DefaultHttpClient httpClient = new DefaultHttpClient();
	private int responseCode;


	@Override

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);


		TextView tv1 = (TextView)findViewById(R.id.text1);
		TextView tv3 = (TextView)findViewById(R.id.text3);




		try {


			HttpGet httpGet = new HttpGet (&quot;http://myURL&quot;);
		
			httpGet.setHeader(&quot;Host&quot;, &quot;myhost&quot;);
			httpGet.setHeader(&quot;Authorization&quot;, &quot;username:password&quot;);
			httpGet.setHeader(&quot;Content-Type&quot;,&quot;Content-Type: application/xml; charset=utf-8&quot;);


			HttpResponse httpResponse = httpClient.execute(httpGet);
			responseCode = httpResponse.getStatusLine().getStatusCode();
		
		
			if(responseCode == 200){
			HttpEntity entity = httpResponse.getEntity();
			String xml = EntityUtils.toString(entity);
			httpResponse = httpClient.execute(httpGet);
		
		
			httpResponse.getEntity();
			tv3.setText(xml.toString());
			String reden = httpResponse.getStatusLine().getReasonPhrase();
			tv1.setText(&quot;\n\nHTTP Statuscode : &quot; + responseCode+&quot; &quot;+reden);
			}else{
		
			String reden = httpResponse.getStatusLine().getReasonPhrase();
			throw new RuntimeException(&quot;Problem reading status (code=&quot;+responseCode +&quot;):&quot;+reden);
		
			}
		
			} catch (ClientProtocolException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();

			}
					
	}
}





//Post part


	private DefaultHttpClient httpClient = new DefaultHttpClient();
	private int responseCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        
        TextView tv1 = (TextView)findViewById(R.id.text1);
    	TextView tv2 = (TextView)findViewById(R.id.text2);
    	TextView tv3 = (TextView)findViewById(R.id.text3);
 			
 		try {
 	 		HttpPost httpPost = new HttpPost(&quot;http://myURL&quot;);
 	 		httpPost.addHeader(&quot;Host&quot;,&quot;myhost.com&quot;);
 	 		httpPost.addHeader(&quot;Authorization&quot;, &quot; username:password&quot;);
 	 		httpPost.addHeader(&quot;Content-Type&quot;,&quot;application/xml; charset=utf-8&quot;);
 			HttpResponse httpResponse = httpClient.execute(httpPost);
 			responseCode = httpResponse.getStatusLine().getStatusCode();
 			
 			if(responseCode == 200){
 				HttpEntity entity = httpResponse.getEntity();
 				String xml = EntityUtils.toString(entity);
 				httpResponse = httpClient.execute(httpPost);
 			
 			
 				httpResponse.getEntity();
 				tv3.setText(xml.toString());
 				String reason = httpResponse.getStatusLine().getReasonPhrase();
 				tv1.setText(&quot;\n\nHTTP Statuscode : &quot; + responseCode+  &quot; &quot;+reason);
 				}else{
 				
 				String reason = httpResponse.getStatusLine().getReasonPhrase();
 				tv2.setText(&quot;\n\nHTTP Statuscode : &quot; + responseCode+&quot; &quot;+reason);
 				throw new RuntimeException(&quot;Problemen bij het lezen van het status (code=&quot;+responseCode +&quot;):&quot;+reason);
 				
 		}
 		   			
 				InputStream in = httpResponse.getEntity().getContent();
 				//BufferedReader reader = new BufferedReader(new InputStreamReader(in));
 				StringBuilder str = new StringBuilder();
 				String line = null;
 				
 		}catch (IOException e){ 
 			//
 		} catch (Exception e1) {
 			//
 		    
 		
 			}	
 		}}

Thanks in advance</description>
		<content:encoded><![CDATA[<p>Hi Anthony,<br />
I am having trouble reading a plain xml file(REST web service).</p>
<p>I have an GET(see code below) Method here(Getting the right result: HTTP status + xml).<br />
However when trying with an POST Method i only get the HTTP status, but the xml file are not displayed at the screen.</p>
<p>Can you help me figure out what am I doing wrong on the POST part.</p>
<p>Notice! I have two service one that requires GET and the other POST<br />
// This is the GET method part<br />
private DefaultHttpClient httpClient = new DefaultHttpClient();<br />
	private int responseCode;</p>
<p>	@Override</p>
<p>	public void onCreate(Bundle savedInstanceState) {<br />
		super.onCreate(savedInstanceState);<br />
		setContentView(R.layout.main);</p>
<p>		TextView tv1 = (TextView)findViewById(R.id.text1);<br />
		TextView tv3 = (TextView)findViewById(R.id.text3);</p>
<p>		try {</p>
<p>			HttpGet httpGet = new HttpGet (&laquo;&nbsp;http://myURL&nbsp;&raquo;);</p>
<p>			httpGet.setHeader(&laquo;&nbsp;Host&nbsp;&raquo;, &laquo;&nbsp;myhost&nbsp;&raquo;);<br />
			httpGet.setHeader(&laquo;&nbsp;Authorization&nbsp;&raquo;, &laquo;&nbsp;username:password&nbsp;&raquo;);<br />
			httpGet.setHeader(&laquo;&nbsp;Content-Type&nbsp;&raquo;,&nbsp;&raquo;Content-Type: application/xml; charset=utf-8&#8243;);</p>
<p>			HttpResponse httpResponse = httpClient.execute(httpGet);<br />
			responseCode = httpResponse.getStatusLine().getStatusCode();</p>
<p>			if(responseCode == 200){<br />
			HttpEntity entity = httpResponse.getEntity();<br />
			String xml = EntityUtils.toString(entity);<br />
			httpResponse = httpClient.execute(httpGet);</p>
<p>			httpResponse.getEntity();<br />
			tv3.setText(xml.toString());<br />
			String reden = httpResponse.getStatusLine().getReasonPhrase();<br />
			tv1.setText(&laquo;&nbsp;\n\nHTTP Statuscode : &nbsp;&raquo; + responseCode+&nbsp;&raquo; &laquo;&nbsp;+reden);<br />
			}else{</p>
<p>			String reden = httpResponse.getStatusLine().getReasonPhrase();<br />
			throw new RuntimeException(&laquo;&nbsp;Problem reading status (code=&nbsp;&raquo;+responseCode +&nbsp;&raquo;):&nbsp;&raquo;+reden);</p>
<p>			}</p>
<p>			} catch (ClientProtocolException e1) {<br />
				// TODO Auto-generated catch block<br />
				e1.printStackTrace();<br />
				} catch (IOException e1) {<br />
				// TODO Auto-generated catch block<br />
				e1.printStackTrace();</p>
<p>			}</p>
<p>	}<br />
}</p>
<p>//Post part</p>
<p>	private DefaultHttpClient httpClient = new DefaultHttpClient();<br />
	private int responseCode;<br />
    @Override<br />
    public void onCreate(Bundle savedInstanceState) {<br />
        super.onCreate(savedInstanceState);<br />
        setContentView(R.layout.main);        </p>
<p>        TextView tv1 = (TextView)findViewById(R.id.text1);<br />
    	TextView tv2 = (TextView)findViewById(R.id.text2);<br />
    	TextView tv3 = (TextView)findViewById(R.id.text3);</p>
<p> 		try {<br />
 	 		HttpPost httpPost = new HttpPost(&laquo;&nbsp;http://myURL&nbsp;&raquo;);<br />
 	 		httpPost.addHeader(&laquo;&nbsp;Host&nbsp;&raquo;,&nbsp;&raquo;myhost.com&nbsp;&raquo;);<br />
 	 		httpPost.addHeader(&laquo;&nbsp;Authorization&nbsp;&raquo;, &nbsp;&raquo; username:password&nbsp;&raquo;);<br />
 	 		httpPost.addHeader(&laquo;&nbsp;Content-Type&nbsp;&raquo;,&nbsp;&raquo;application/xml; charset=utf-8&#8243;);<br />
 			HttpResponse httpResponse = httpClient.execute(httpPost);<br />
 			responseCode = httpResponse.getStatusLine().getStatusCode();</p>
<p> 			if(responseCode == 200){<br />
 				HttpEntity entity = httpResponse.getEntity();<br />
 				String xml = EntityUtils.toString(entity);<br />
 				httpResponse = httpClient.execute(httpPost);</p>
<p> 				httpResponse.getEntity();<br />
 				tv3.setText(xml.toString());<br />
 				String reason = httpResponse.getStatusLine().getReasonPhrase();<br />
 				tv1.setText(&laquo;&nbsp;\n\nHTTP Statuscode : &nbsp;&raquo; + responseCode+  &nbsp;&raquo; &laquo;&nbsp;+reason);<br />
 				}else{</p>
<p> 				String reason = httpResponse.getStatusLine().getReasonPhrase();<br />
 				tv2.setText(&laquo;&nbsp;\n\nHTTP Statuscode : &nbsp;&raquo; + responseCode+&nbsp;&raquo; &laquo;&nbsp;+reason);<br />
 				throw new RuntimeException(&laquo;&nbsp;Problemen bij het lezen van het status (code=&nbsp;&raquo;+responseCode +&nbsp;&raquo;):&nbsp;&raquo;+reason);</p>
<p> 		}</p>
<p> 				InputStream in = httpResponse.getEntity().getContent();<br />
 				//BufferedReader reader = new BufferedReader(new InputStreamReader(in));<br />
 				StringBuilder str = new StringBuilder();<br />
 				String line = null;</p>
<p> 		}catch (IOException e){<br />
 			//<br />
 		} catch (Exception e1) {<br />
 			//</p>
<p> 			}<br />
 		}}</p>
<p>Thanks in advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Anthony</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31344</link>
		<dc:creator>Anthony</dc:creator>
		<pubDate>Wed, 13 Apr 2011 14:11:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31344</guid>
		<description>you&#039;re welcome; you&#039;re right, probably not that safe to accept all the certificates; but very convenient... You have to choose your approach : a most complete one, with a GUI to deal with all the certificates; or the quick and dirty one, that allows HTTPS user to have a first working version, in no time. It&#039;s your call !</description>
		<content:encoded><![CDATA[<p>you&#8217;re welcome; you&#8217;re right, probably not that safe to accept all the certificates; but very convenient&#8230; You have to choose your approach : a most complete one, with a GUI to deal with all the certificates; or the quick and dirty one, that allows HTTPS user to have a first working version, in no time. It&#8217;s your call !</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Tommy</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31343</link>
		<dc:creator>Tommy</dc:creator>
		<pubDate>Wed, 13 Apr 2011 13:41:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31343</guid>
		<description>hello Anthony, thanks for you quick reply

i&#039;ve take a look the link that you give to me, it seems that whatever the certificate it will always validate it &quot;true&quot;. is that really safe to do it? is there another ways to validate the certificate? thanks for your help, i really apreciate that</description>
		<content:encoded><![CDATA[<p>hello Anthony, thanks for you quick reply</p>
<p>i&#8217;ve take a look the link that you give to me, it seems that whatever the certificate it will always validate it &laquo;&nbsp;true&nbsp;&raquo;. is that really safe to do it? is there another ways to validate the certificate? thanks for your help, i really apreciate that</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : anthony.dahanne</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31336</link>
		<dc:creator>anthony.dahanne</dc:creator>
		<pubDate>Wed, 13 Apr 2011 00:40:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31336</guid>
		<description>hello Tommy,
for HTTPS, you have to play with ssl keystore.
some stuff I did for RegalAndroid, for G2 :
// avoid instanciating
SchemeRegistry schemeRegistry = new SchemeRegistry();
// http scheme
schemeRegistry.register(new Scheme(&quot;http&quot;, PlainSocketFactory
.getSocketFactory(), 80));
// https scheme
schemeRegistry.register(new Scheme(&quot;https&quot;, new FakeSocketFactory(),
443));

you can have a look at the class there : 
https://github.com/anthonydahanne/ReGalAndroid/blob/master/g2-java-client/src/main/java/net/dahanne/gallery/g2/java/client/business/G2Client.java

Thanks for answering some comments ;-)</description>
		<content:encoded><![CDATA[<p>hello Tommy,<br />
for HTTPS, you have to play with ssl keystore.<br />
some stuff I did for RegalAndroid, for G2 :<br />
// avoid instanciating<br />
SchemeRegistry schemeRegistry = new SchemeRegistry();<br />
// http scheme<br />
schemeRegistry.register(new Scheme(&laquo;&nbsp;http&nbsp;&raquo;, PlainSocketFactory<br />
.getSocketFactory(), 80));<br />
// https scheme<br />
schemeRegistry.register(new Scheme(&laquo;&nbsp;https&nbsp;&raquo;, new FakeSocketFactory(),<br />
443));</p>
<p>you can have a look at the class there :<br />
<a href="https://github.com/anthonydahanne/ReGalAndroid/blob/master/g2-java-client/src/main/java/net/dahanne/gallery/g2/java/client/business/G2Client.java" rel="nofollow">https://github.com/anthonydahanne/ReGalAndroid/blob/master/g2-java-client/src/main/java/net/dahanne/gallery/g2/java/client/business/G2Client.java</a></p>
<p>Thanks for answering some comments <img src='http://blog.dahanne.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : tommy</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31334</link>
		<dc:creator>tommy</dc:creator>
		<pubDate>Tue, 12 Apr 2011 21:06:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31334</guid>
		<description>hi Anthoni.. your post was great.. but i think u miss the answer for several question :)
i&#039;ll help you to answer some question :)

@Rui, you can add the XML to the parameter. 
i.e
nameValuePairs.add(new BasicNameValuePair(, myXML));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

make sure you convert the XML to a String myXML.. hope this usefull


@Anthoni
can you give me sample code, or explanation about HTTPS with Android??
it seems

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List nameValuePairs = new ArrayList(1);
//this is where you add your data to the post method
nameValuePairs.add(new BasicNameValuePair(
« name », « anthony »));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpPost);
content = response.getEntity().getContent();

it can only work with HTTP, i dont know how to get it work on HTTPS, for note i&#039;m using GLASSFISH default key for HTTPS (https:localhost/MyWeb:8181)

thx before</description>
		<content:encoded><![CDATA[<p>hi Anthoni.. your post was great.. but i think u miss the answer for several question <img src='http://blog.dahanne.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
i&#8217;ll help you to answer some question <img src='http://blog.dahanne.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Rui, you can add the XML to the parameter.<br />
i.e<br />
nameValuePairs.add(new BasicNameValuePair(, myXML));<br />
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));</p>
<p>make sure you convert the XML to a String myXML.. hope this usefull</p>
<p>@Anthoni<br />
can you give me sample code, or explanation about HTTPS with Android??<br />
it seems</p>
<p>HttpClient httpclient = new DefaultHttpClient();<br />
HttpPost httpPost = new HttpPost(url);<br />
List nameValuePairs = new ArrayList(1);<br />
//this is where you add your data to the post method<br />
nameValuePairs.add(new BasicNameValuePair(<br />
« name », « anthony »));<br />
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));<br />
// Execute HTTP Post Request<br />
HttpResponse response = httpclient.execute(httpPost);<br />
content = response.getEntity().getContent();</p>
<p>it can only work with HTTP, i dont know how to get it work on HTTPS, for note i&#8217;m using GLASSFISH default key for HTTPS (https:localhost/MyWeb:8181)</p>
<p>thx before</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : sam</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31314</link>
		<dc:creator>sam</dc:creator>
		<pubDate>Fri, 08 Apr 2011 12:05:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31314</guid>
		<description>Hi Anthony, 

Nice tutorial ... I am having a problem though ... I am usind http post to login to a website. I am sending username and password as key value pairs. But i am getting an empty response. I cant figure out the problem. Can you please help me out?</description>
		<content:encoded><![CDATA[<p>Hi Anthony, </p>
<p>Nice tutorial &#8230; I am having a problem though &#8230; I am usind http post to login to a website. I am sending username and password as key value pairs. But i am getting an empty response. I cant figure out the problem. Can you please help me out?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : Rooban</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31299</link>
		<dc:creator>Rooban</dc:creator>
		<pubDate>Tue, 05 Apr 2011 09:56:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31299</guid>
		<description>Nice Tutorial....</description>
		<content:encoded><![CDATA[<p>Nice Tutorial&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Par : xesun</title>
		<link>http://blog.dahanne.net/2009/08/16/how-to-access-http-resources-from-android/comment-page-1/#comment-31258</link>
		<dc:creator>xesun</dc:creator>
		<pubDate>Tue, 29 Mar 2011 01:26:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dahanne.net/?p=227#comment-31258</guid>
		<description>hello i am mongolia. 
Android question:
   interface</description>
		<content:encoded><![CDATA[<p>hello i am mongolia.<br />
Android question:<br />
   interface</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic

Served from: blog.dahanne.net @ 2012-02-09 11:31:50 -->
