[android-developers] Re: HTTP Post
Here is some code that I am using in my app that is using HttpAuth and was a JSON client, I'm sure you can easily configure it to your needs. If you need anymore help just ask :-) public static final String HORDE_URI = "http://10.0.0.60:80";; //<-- base url public static final String RPC_PATH = "/mail/rpc.php"; //<--path if required public static final String USER_NAME = "test"; public static final String PASSWORD = "test123"; Credentials loginCredentials = new UsernamePasswordCredentials (USER_NAME, PASSWORD); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); credsProvider.setCredentials(authScope, loginCredentials); AbstractHttpClient httpClient = new DefaultHttpClient(); httpClient.setCredentialsProvider(credsProvider); HttpPost postMethod = new HttpPost(mUrl); StringEntity reqEntity = new StringEntity(REQUESTGOESHERE); reqEntity.setChunked(false); reqEntity.setContentType("application/json"); postMethod.setEntity(reqEntity); Thanks, Aakash Patel Patel.io On Apr 19, 5:36 pm, xspotlivin wrote: > I'm trying to do an HTTP Post, but I'm having a lot of trouble. I have > a server running on my computer. I know the URL needs to be my IP > address and port number. How do you create a URL object out of an IP > address and port number? I also have some headers and such that I need > to put on the post message. I haven't seen anyone doing a simple HTTP > post on Android. Can anyone help me? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: HTTP Post
Here is some code that I am using in my app that is using HttpAuth and was a JSON client, I'm sure you can easily configure it to your needs. If you need anymore help just ask :-) public static final String HORDE_URI = "http://10.0.0.60:80";; public static final String RPC_PATH = "/mail/rpc.php"; public static final String USER_NAME = "test"; public static final String PASSWORD = "test123"; String mUrl = HORDE_URI + RPC_PATH; Credentials loginCredentials = new UsernamePasswordCredentials (USER_NAME, PASSWORD); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); credsProvider.setCredentials(authScope, loginCredentials); AbstractHttpClient httpClient = new DefaultHttpClient(); httpClient.setCredentialsProvider(credsProvider); HttpPost postMethod = new HttpPost(mUrl); StringEntity reqEntity = new StringEntity(REQUESTGOESHERE); reqEntity.setChunked(false); reqEntity.setContentType("application/json"); postMethod.setEntity(reqEntity); Thanks, Aakash Patel On Apr 19, 5:36 pm, xspotlivin wrote: > I'm trying to do an HTTP Post, but I'm having a lot of trouble. I have > a server running on my computer. I know the URL needs to be my IP > address and port number. How do you create a URL object out of an IP > address and port number? I also have some headers and such that I need > to put on the post message. I haven't seen anyone doing a simple HTTP > post on Android. Can anyone help me? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: HTTP Post
Here is some code that I am using in my app that is using HttpAuth and was a JSON client, It should be very easy to configure it to your needs. public static final String HORDE_URI = "http://10.0.0.60:80";; public static final String RPC_PATH = "/mail/rpc.php"; public static final String USER_NAME = "test"; public static final String PASSWORD = "test123"; String mUrl = HORDE_URI + RPC_PATH; Credentials loginCredentials = new UsernamePasswordCredentials (USER_NAME, PASSWORD); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); credsProvider.setCredentials(authScope, loginCredentials); AbstractHttpClient httpClient = new DefaultHttpClient(); httpClient.setCredentialsProvider(credsProvider); HttpPost postMethod = new HttpPost(mUrl); StringEntity reqEntity = new StringEntity(REQUESTGOESHERE); //put request string here reqEntity.setChunked(false); reqEntity.setContentType("application/json"); postMethod.setEntity(reqEntity); httpClient.execute(postMethod); I have some code to get the response from the server, if you need it just ask :-) Thanks, Aakash Patel On Apr 19, 5:36 pm, xspotlivin wrote: > I'm trying to do an HTTP Post, but I'm having a lot of trouble. I have > a server running on my computer. I know the URL needs to be my IP > address and port number. How do you create a URL object out of an IP > address and port number? I also have some headers and such that I need > to put on the post message. I haven't seen anyone doing a simple HTTP > post on Android. Can anyone help me? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: HTTP Post
Here is some code that I am using in my app that is using HttpAuth and was a JSON RPC client, It should be very easy to configure it to your needs. public static final String HORDE_URI = "http://10.0.0.60:80";; public static final String RPC_PATH = "/mail/rpc.php"; public static final String USER_NAME = "test"; public static final String PASSWORD = "test123"; String mUrl = HORDE_URI + RPC_PATH; Credentials loginCredentials = new UsernamePasswordCredentials (USER_NAME, PASSWORD); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); credsProvider.setCredentials(authScope, loginCredentials); AbstractHttpClient httpClient = new DefaultHttpClient(); httpClient.setCredentialsProvider(credsProvider); HttpPost postMethod = new HttpPost(mUrl); StringEntity reqEntity = new StringEntity(REQUESTGOESHERE); //put request string here reqEntity.setChunked(false); reqEntity.setContentType("application/json"); postMethod.setEntity(reqEntity); httpClient.execute(postMethod); I have some code to get the response from the server, if you need it just ask :-) Thanks, Aakash Patel On Apr 19, 5:36 pm, xspotlivin wrote: > I'm trying to do an HTTP Post, but I'm having a lot of trouble. I have > a server running on my computer. I know the URL needs to be my IP > address and port number. How do you create a URL object out of an IP > address and port number? I also have some headers and such that I need > to put on the post message. I haven't seen anyone doing a simple HTTP > post on Android. Can anyone help me? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: HTTP POST
if you are testing this on your emulator then you have to change localserver to your ip because emulator is it self a localserver. open your cmd and write "ipconfig/all" tp get your system ip and then place your ip instead of localhost and it should work this time :) Thanks On Sep 21, 10:11 pm, John Gaby wrote: > I am trying to do an HTTP POST of some data. The following is a test > routine that I wrote to try and accomplish this: > > void Put() > { > HttpURLConnection connection = null; > DataOutputStream outputStream = null; > > String urlServer = > "http://localhost:3571/mycmd.aspx?command=test";; > String message = "This is a message"; > > try > { > URL url = new URL(urlServer); > connection = (HttpURLConnection) url.openConnection(); > > // Allow Inputs & Outputs > connection.setDoInput(true); > connection.setDoOutput(true); > connection.setUseCaches(false); > > // Enable POST method > connection.setRequestMethod("POST"); > > connection.setRequestProperty("Connection", > "Keep-Alive"); > connection.setRequestProperty("Content-Type", > "application/octet- > stream"); > > outputStream = new > DataOutputStream( connection.getOutputStream() ); > outputStream.writeBytes(message); > > // Responses from the server (code and message) > int serverResponseCode = connection.getResponseCode(); > String serverResponseMessage = > connection.getResponseMessage(); > > outputStream.flush(); > outputStream.close(); > } > catch (Exception ex) > { > int i = 0; > //Exception handling > } > } > > If I set urlServer to 'localhost' I get an exception: > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > refused > > I am presuming that this is because the localhost is not being > resolved properly to my host machine (I am running under the > emulator). Is there a way to access a localhost server on my host > machine from the emulator, or do I need a real IP address? > > Thanks -- 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
[android-developers] Re: HTTP POST
Thanks for the reply. I have tried using the IP address of the host machine like this: String urlServer = "http://192.168.1.101:3571/mycmd.aspx? command=test"; but when I do that, my program times out on the 'connection.getOutputStream' call. I am also not sure I see how this would work anyway, since I an not able to access my local server by using that IP address, even from a browser. Thanks On Sep 21, 11:22 am, Hotouch wrote: > if you are testing this on your emulator then you have to change > localserver to your ip because emulator is it self a localserver. > > open your cmd and write "ipconfig/all" tp get your system ip and then > place your ip instead of localhost and it should work this time :) > > Thanks > > On Sep 21, 10:11 pm, John Gaby wrote: > > > I am trying to do an HTTP POST of some data. The following is a test > > routine that I wrote to try and accomplish this: > > > void Put() > > { > > HttpURLConnection connection = null; > > DataOutputStream outputStream = null; > > > String urlServer = > > "http://localhost:3571/mycmd.aspx?command=test";; > > String message = "This is a message"; > > > try > > { > > URL url = new URL(urlServer); > > connection = (HttpURLConnection) > > url.openConnection(); > > > // Allow Inputs & Outputs > > connection.setDoInput(true); > > connection.setDoOutput(true); > > connection.setUseCaches(false); > > > // Enable POST method > > connection.setRequestMethod("POST"); > > > connection.setRequestProperty("Connection", > > "Keep-Alive"); > > connection.setRequestProperty("Content-Type", > > "application/octet- > > stream"); > > > outputStream = new > > DataOutputStream( connection.getOutputStream() ); > > outputStream.writeBytes(message); > > > // Responses from the server (code and message) > > int serverResponseCode = > > connection.getResponseCode(); > > String serverResponseMessage = > > connection.getResponseMessage(); > > > outputStream.flush(); > > outputStream.close(); > > } > > catch (Exception ex) > > { > > int i = 0; > > //Exception handling > > } > > } > > > If I set urlServer to 'localhost' I get an exception: > > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > > refused > > > I am presuming that this is because the localhost is not being > > resolved properly to my host machine (I am running under the > > emulator). Is there a way to access a localhost server on my host > > machine from the emulator, or do I need a real IP address? > > > Thanks -- 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
[android-developers] Re: HTTP POST
Check if your server is responding or not. You should probably look at the "Network Space Address" on this page. http://developer.android.com/guide/developing/tools/emulator.html -Kumar Bibek http://techdroid.kbeanie.com On Sep 22, 12:18 am, John Gaby wrote: > Thanks for the reply. I have tried using the IP address of the host > machine like this: > > String urlServer = "http://192.168.1.101:3571/mycmd.aspx? > command=test"; > > but when I do that, my program times out on the > 'connection.getOutputStream' call. I am also not sure I see how this > would work anyway, since I an not able to access my local server by > using that IP address, even from a browser. > > Thanks > > On Sep 21, 11:22 am, Hotouch wrote: > > > if you are testing this on your emulator then you have to change > > localserver to your ip because emulator is it self a localserver. > > > open your cmd and write "ipconfig/all" tp get your system ip and then > > place your ip instead of localhost and it should work this time :) > > > Thanks > > > On Sep 21, 10:11 pm, John Gaby wrote: > > > > I am trying to do an HTTP POST of some data. The following is a test > > > routine that I wrote to try and accomplish this: > > > > void Put() > > > { > > > HttpURLConnection connection = null; > > > DataOutputStream outputStream = null; > > > > String urlServer = > > > "http://localhost:3571/mycmd.aspx?command=test";; > > > String message = "This is a message"; > > > > try > > > { > > > URL url = new URL(urlServer); > > > connection = (HttpURLConnection) > > > url.openConnection(); > > > > // Allow Inputs & Outputs > > > connection.setDoInput(true); > > > connection.setDoOutput(true); > > > connection.setUseCaches(false); > > > > // Enable POST method > > > connection.setRequestMethod("POST"); > > > > connection.setRequestProperty("Connection", > > > "Keep-Alive"); > > > connection.setRequestProperty("Content-Type", > > > "application/octet- > > > stream"); > > > > outputStream = new > > > DataOutputStream( connection.getOutputStream() ); > > > outputStream.writeBytes(message); > > > > // Responses from the server (code and message) > > > int serverResponseCode = > > > connection.getResponseCode(); > > > String serverResponseMessage = > > > connection.getResponseMessage(); > > > > outputStream.flush(); > > > outputStream.close(); > > > } > > > catch (Exception ex) > > > { > > > int i = 0; > > > //Exception handling > > > } > > > } > > > > If I set urlServer to 'localhost' I get an exception: > > > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > > > refused > > > > I am presuming that this is because the localhost is not being > > > resolved properly to my host machine (I am running under the > > > emulator). Is there a way to access a localhost server on my host > > > machine from the emulator, or do I need a real IP address? > > > > Thanks -- 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
[android-developers] Re: HTTP POST
The following address from the emulator docs might be what you want: >10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 >on your development machine) Test it in the emulator's android browser before worrying about your code On Sep 21, 3:18 pm, John Gaby wrote: > Thanks for the reply. I have tried using the IP address of the host > machine like this: > > String urlServer = "http://192.168.1.101:3571/mycmd.aspx? > command=test"; > > but when I do that, my program times out on the > 'connection.getOutputStream' call. I am also not sure I see how this > would work anyway, since I an not able to access my local server by > using that IP address, even from a browser. > > Thanks > > On Sep 21, 11:22 am, Hotouch wrote: > > > if you are testing this on your emulator then you have to change > > localserver to your ip because emulator is it self a localserver. > > > open your cmd and write "ipconfig/all" tp get your system ip and then > > place your ip instead of localhost and it should work this time :) > > > Thanks > > > On Sep 21, 10:11 pm, John Gaby wrote: > > > > I am trying to do an HTTP POST of some data. The following is a test > > > routine that I wrote to try and accomplish this: > > > > void Put() > > > { > > > HttpURLConnection connection = null; > > > DataOutputStream outputStream = null; > > > > String urlServer = > > > "http://localhost:3571/mycmd.aspx?command=test";; > > > String message = "This is a message"; > > > > try > > > { > > > URL url = new URL(urlServer); > > > connection = (HttpURLConnection) > > > url.openConnection(); > > > > // Allow Inputs & Outputs > > > connection.setDoInput(true); > > > connection.setDoOutput(true); > > > connection.setUseCaches(false); > > > > // Enable POST method > > > connection.setRequestMethod("POST"); > > > > connection.setRequestProperty("Connection", > > > "Keep-Alive"); > > > connection.setRequestProperty("Content-Type", > > > "application/octet- > > > stream"); > > > > outputStream = new > > > DataOutputStream( connection.getOutputStream() ); > > > outputStream.writeBytes(message); > > > > // Responses from the server (code and message) > > > int serverResponseCode = > > > connection.getResponseCode(); > > > String serverResponseMessage = > > > connection.getResponseMessage(); > > > > outputStream.flush(); > > > outputStream.close(); > > > } > > > catch (Exception ex) > > > { > > > int i = 0; > > > //Exception handling > > > } > > > } > > > > If I set urlServer to 'localhost' I get an exception: > > > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > > > refused > > > > I am presuming that this is because the localhost is not being > > > resolved properly to my host machine (I am running under the > > > emulator). Is there a way to access a localhost server on my host > > > machine from the emulator, or do I need a real IP address? > > > > Thanks > > -- 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
[android-developers] Re: HTTP POST
You said, that you cannot open the local server from the browser Surely, a problem with the server I suspect. -Kumar Bibek http://techdroid.kbeanie.com On Sep 22, 12:18 am, John Gaby wrote: > Thanks for the reply. I have tried using the IP address of the host > machine like this: > > String urlServer = "http://192.168.1.101:3571/mycmd.aspx? > command=test"; > > but when I do that, my program times out on the > 'connection.getOutputStream' call. I am also not sure I see how this > would work anyway, since I an not able to access my local server by > using that IP address, even from a browser. > > Thanks > > On Sep 21, 11:22 am, Hotouch wrote: > > > if you are testing this on your emulator then you have to change > > localserver to your ip because emulator is it self a localserver. > > > open your cmd and write "ipconfig/all" tp get your system ip and then > > place your ip instead of localhost and it should work this time :) > > > Thanks > > > On Sep 21, 10:11 pm, John Gaby wrote: > > > > I am trying to do an HTTP POST of some data. The following is a test > > > routine that I wrote to try and accomplish this: > > > > void Put() > > > { > > > HttpURLConnection connection = null; > > > DataOutputStream outputStream = null; > > > > String urlServer = > > > "http://localhost:3571/mycmd.aspx?command=test";; > > > String message = "This is a message"; > > > > try > > > { > > > URL url = new URL(urlServer); > > > connection = (HttpURLConnection) > > > url.openConnection(); > > > > // Allow Inputs & Outputs > > > connection.setDoInput(true); > > > connection.setDoOutput(true); > > > connection.setUseCaches(false); > > > > // Enable POST method > > > connection.setRequestMethod("POST"); > > > > connection.setRequestProperty("Connection", > > > "Keep-Alive"); > > > connection.setRequestProperty("Content-Type", > > > "application/octet- > > > stream"); > > > > outputStream = new > > > DataOutputStream( connection.getOutputStream() ); > > > outputStream.writeBytes(message); > > > > // Responses from the server (code and message) > > > int serverResponseCode = > > > connection.getResponseCode(); > > > String serverResponseMessage = > > > connection.getResponseMessage(); > > > > outputStream.flush(); > > > outputStream.close(); > > > } > > > catch (Exception ex) > > > { > > > int i = 0; > > > //Exception handling > > > } > > > } > > > > If I set urlServer to 'localhost' I get an exception: > > > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > > > refused > > > > I am presuming that this is because the localhost is not being > > > resolved properly to my host machine (I am running under the > > > emulator). Is there a way to access a localhost server on my host > > > machine from the emulator, or do I need a real IP address? > > > > Thanks -- 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
[android-developers] Re: HTTP POST
I checked out the link you provided and found that I needed to use the IP address 10.0.2.2 to access the host's loopback interface. I changed my request to us that IP and it works like a champ. Thanks much! On Sep 21, 12:28 pm, Kumar Bibek wrote: > Check if your server is responding or not. > > You should probably look at the "Network Space Address" on this > page.http://developer.android.com/guide/developing/tools/emulator.html > > -Kumar Bibekhttp://techdroid.kbeanie.com > > On Sep 22, 12:18 am, John Gaby wrote: > > > Thanks for the reply. I have tried using the IP address of the host > > machine like this: > > > String urlServer = "http://192.168.1.101:3571/mycmd.aspx? > > command=test"; > > > but when I do that, my program times out on the > > 'connection.getOutputStream' call. I am also not sure I see how this > > would work anyway, since I an not able to access my local server by > > using that IP address, even from a browser. > > > Thanks > > > On Sep 21, 11:22 am, Hotouch wrote: > > > > if you are testing this on your emulator then you have to change > > > localserver to your ip because emulator is it self a localserver. > > > > open your cmd and write "ipconfig/all" tp get your system ip and then > > > place your ip instead of localhost and it should work this time :) > > > > Thanks > > > > On Sep 21, 10:11 pm, John Gaby wrote: > > > > > I am trying to do an HTTP POST of some data. The following is a test > > > > routine that I wrote to try and accomplish this: > > > > > void Put() > > > > { > > > > HttpURLConnection connection = null; > > > > DataOutputStream outputStream = null; > > > > > String urlServer = > > > > "http://localhost:3571/mycmd.aspx?command=test";; > > > > String message = "This is a message"; > > > > > try > > > > { > > > > URL url = new URL(urlServer); > > > > connection = (HttpURLConnection) > > > > url.openConnection(); > > > > > // Allow Inputs & Outputs > > > > connection.setDoInput(true); > > > > connection.setDoOutput(true); > > > > connection.setUseCaches(false); > > > > > // Enable POST method > > > > connection.setRequestMethod("POST"); > > > > > connection.setRequestProperty("Connection", > > > > "Keep-Alive"); > > > > connection.setRequestProperty("Content-Type", > > > > "application/octet- > > > > stream"); > > > > > outputStream = new > > > > DataOutputStream( connection.getOutputStream() ); > > > > outputStream.writeBytes(message); > > > > > // Responses from the server (code and message) > > > > int serverResponseCode = > > > > connection.getResponseCode(); > > > > String serverResponseMessage = > > > > connection.getResponseMessage(); > > > > > outputStream.flush(); > > > > outputStream.close(); > > > > } > > > > catch (Exception ex) > > > > { > > > > int i = 0; > > > > //Exception handling > > > > } > > > > } > > > > > If I set urlServer to 'localhost' I get an exception: > > > > > java.net.ConnectException: localhost/127.0.0.1:3571 - Connection > > > > refused > > > > > I am presuming that this is because the localhost is not being > > > > resolved properly to my host machine (I am running under the > > > > emulator). Is there a way to access a localhost server on my host > > > > machine from the emulator, or do I need a real IP address? > > > > > Thanks -- 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
[android-developers] Re: http POST message
Use StringEntity class to generate a body which you can attach to the post method. http://icloud.com/wiki/index.php/A_Android_tutorial_to_get_you_started This example will get you started. Thanks and Regards, Kumar Bibek http://tech-droid.blogspot.com On Mar 29, 3:19 pm, MobDev wrote: > Hi. > I ahve been trying to send a http POST message towards my server, I > have been using this code : > > HttpClient httpclient = new DefaultHttpClient(); > HttpPost httppost = new HttpPost(sUrl); > > // Add your data > List nameValuePairs = new > ArrayList(1); > String userAgent = Management.getInstance().getUserAgent(); > nameValuePairs.add(new BasicNameValuePair("User-Agent", > "Test_User_Agent")); > nameValuePairs.add(new BasicNameValuePair("Data", data)); > httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); > > Honestly, to me it all seems overly complicated > To start off, how do I actually add a body (for example a custom XML) > to the post ? > Is the nameValuePairs the only option ? I'd like to ismply add a POST > body with data without having to "name" it with a key... > Any help or tips would be greatly appreciated... -- 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 To unsubscribe from this group, send email to android-developers+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
[android-developers] Re: http post NetworkOnMainThreadException
After gingerbread android sdk doest allow having networking work on main thread UI one On Thursday, 25 October 2012 16:40:23 UTC+5:30, aueddonline wrote: > > Below I have some of my MainActivity class which starts by making a http > post request to the web server running on my development laptop. The > webpage outputs some JSON also shown below; However I am getting > a NetworkOnMainThreadException when running the app. I can go into the > browser within the emulator and bring up the php page no problem. I also > have the following permission set in my manifest xml; * android:name="android.permission.INTERNET">*. Any Help > out there? > > > [{"city_id":"1","city_name":"Adotol"},{"city_id":"2","city_name":"Aberdeen"}][{"city_id":"1","city_name":"Adotol"},{"city_id":"2","city_name":"Aberdeen"}] > > > > > public class MainActivity extends ListActivity { > > > JSONArray jArray; > String result = null; > InputStream is = null; > StringBuilder sb=null; > > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > //setContentView(R.id.list); > > > ArrayList nameValuePairs = new ArrayList(); > //http post > try{ > HttpClient httpclient = new DefaultHttpClient(); > String phpaddress = "*http://10.0.2.2/androidselectservice.php*";; > System.out.println("'"+phpaddress+"'"); > HttpPost httppost = new HttpPost(phpaddress); > httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); > HttpResponse response = httpclient.execute(httppost); > HttpEntity entity = response.getEntity(); > is = entity.getContent(); > }catch(Exception e){ > Log.e("log_tag", "Error in http connection "+e.toString()); > } > -- 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
[android-developers] Re: HTTP Post with header
generally: http post requires content-length unless you use chunked encoding. In the example you give set the content-length to the length of your StringEntity. On Dec 17, 4:00 am, agal wrote: > Hi, > > I want to do an HTTP Post in Android, using the HttpClient 4.0 APIs. > This is my code snippet: > ... > HttpClient postclient = new DefaultHttpClient(); > HttpPost postMethod=new HttpPost(posturl); > postMethod.addHeader("Content-Type", "the type"); > String requestBody = "some text"; > postMethod.setEntity(new StringEntity(requestBody)); > postMethod.addHeader("Content-Length", "the length"); > HttpResponse response = null; > try { > response = postclient.execute(postMethod);}catch > (ClientProtocolException cpe) { > > cpe.printStackTrace();} > > catch (IOException e) { > e.printStackTrace();} > > ... > > The above code is giving me ClientProtocolException. And when I do not > add the content length header, the server is rejecting the post > request saying content lenght is missing. > > Any leads on this? Could someone post a working piece of code that > does an HTTP post (with header & body)? > > Thanks. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-developers] Re: http POST request problem
List params = new ArrayList(); params.add(new BasicNameValuePair("A", "B")); even an empty params works fine. but as soon as i add en entry - it fails to response code 400. the weird thing is i copied this code verbatim from the c2dm android sample app - of course i could have done something but i've narrowed it down to one line. the problem happens on both the device and emulator. On Oct 10, 6:53 pm, Brad Gies wrote: > Need to see how you are setting the params to have any idea of how to > help. > > My guess is that you are sending an invalid parameter and the server is > refusing the connection because of that. > > Sincerely, > > Brad Gies > --- > Bistro Bot - Bistro > Blurbhttp://bgies.comhttp://bistroblurb.comhttp://ihottonight.comhttp://forcethetruth.com > --- > > Everything in moderation, including abstinence > > Never doubt that a small group of thoughtful, committed people can > change the world. Indeed. It is the only thing that ever has - Margaret Mead > > On 10/10/2010 11:09 AM, dashman wrote: > > > in the following code - i get an http response code > > 400 - BAD REQUEST. > > > if i comment out the post.setEntity() line - then it > > works fine - response code 200. > > > of course i've got internet permission > > > params is a list of 1 entry - string2string mapping. > > > URI uri = new URI(urlPath); > > > HttpPost post = new HttpPost(uri); > > > UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, > > "UTF-8"); > > > post.setEntity(entity); // if this line is commented out - > > the post results in status 200 OK > > > DefaultHttpClient client = new DefaultHttpClient(); > > > HttpResponse res = client.execute(post); -- 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
[android-developers] Re: http POST request problem
Inorder to pass data using http post we need to set content-Type field in http header. Otherwise the server won't recognize your data which have been sent by parms. Try with the following Example httppost.setHeader("Content-Type","your content type supported by u r server"); On Oct 11, 4:11 am, dashman wrote: > List params = new ArrayList(); > > params.add(new BasicNameValuePair("A", "B")); > > even an empty params works fine. > > but as soon as i add en entry - it fails to response code 400. > > the weird thing is i copied this code verbatim from the c2dm > android sample app - of course i could have done something but > i've narrowed it down to one line. > > the problem happens on both the device and emulator. > > On Oct 10, 6:53 pm, Brad Gies wrote: > > > Need to see how you are setting the params to have any idea of how to > > help. > > > My guess is that you are sending an invalid parameter and the server is > > refusing the connection because of that. > > > Sincerely, > > > Brad Gies > > --- > > Bistro Bot - Bistro > > Blurbhttp://bgies.comhttp://bistroblurb.comhttp://ihottonight.comhttp://fo... > > --- > > > Everything in moderation, including abstinence > > > Never doubt that a small group of thoughtful, committed people can > > change the world. Indeed. It is the only thing that ever has - Margaret Mead > > > On 10/10/2010 11:09 AM, dashman wrote: > > > > in the following code - i get an http response code > > > 400 - BAD REQUEST. > > > > if i comment out the post.setEntity() line - then it > > > works fine - response code 200. > > > > of course i've got internet permission > > > > params is a list of 1 entry - string2string mapping. > > > > URI uri = new URI(urlPath); > > > > HttpPost post = new HttpPost(uri); > > > > UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, > > > "UTF-8"); > > > > post.setEntity(entity); // if this line is commented out - > > > the post results in status 200 OK > > > > DefaultHttpClient client = new DefaultHttpClient(); > > > > HttpResponse res = client.execute(post); > > -- 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
[android-developers] Re: Http post and get
You have to reuse the same cookie store, or better yet, the same http client. -- 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
[android-developers] Re: HTTP Post of Image
Hi Siva, But my application keeps on crashing when I insert this line: URI mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Must I add any permission in the manifest file? Basically I just want to get the string containing the path to the image saved e.g: "/sdcard/DCIM/12345.jpg" Must I use contentprovider? Thanks In Advance, Perumal On Jul 28, 12:23 pm, Sivasankar K wrote: > using the uri of content resolver you can get the path where it is getting > stored > > URI mCapturedImageURI = > getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, > values); > Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); > > intent.putExtra(MediaStore.ACTION_IMAGE_CAPTURE, capturedImage); > intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); > > > > > > > > > > On Thu, Jul 28, 2011 at 8:14 AM, perumal316 wrote: > > Hi All, > > > I am using the following intent to invoke the camera: > > > Intent cameraIntent = new > > Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); > > startActivity(cameraIntent); > > > The image is saved into the sdcard with the time/date as the name. I > > want to do a HTTPPost of this image once the picture is taken. > > > How do I get the name of the picture stored? Or how to I change the > > name of the image stored? > > > Thanks In Advance, > > Perumal > > > -- > > 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 > > -- > K Siva Sankar -- 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] Re: Http post and get
Solved. Thanks On 21 January 2012 05:50, Guus Bloemsma wrote: > You have to reuse the same cookie store, or better yet, the same http > client. > > -- > 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 > -- http://www.uredzadroge.hr/admin/index.php username: atila pass:bicbozji -- 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] Re: HTTP Post of Image
In your manifest file add permissions to camera and for uploading internet permission as follows On Thu, Jul 28, 2011 at 11:07 AM, perumal316 wrote: > Hi Siva, > > But my application keeps on crashing when I insert this line: > URI mCapturedImageURI = > getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, > values); > > Must I add any permission in the manifest file? > > Basically I just want to get the string containing the path to the > image saved e.g: "/sdcard/DCIM/12345.jpg" > > Must I use contentprovider? > > Thanks In Advance, > Perumal > > > > On Jul 28, 12:23 pm, Sivasankar K > wrote: > > using the uri of content resolver you can get the path where it is > getting > > stored > > > > URI mCapturedImageURI = > > getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, > > values); > > Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); > > > > intent.putExtra(MediaStore.ACTION_IMAGE_CAPTURE, > capturedImage); > > intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); > > > > > > > > > > > > > > > > > > > > On Thu, Jul 28, 2011 at 8:14 AM, perumal316 > wrote: > > > Hi All, > > > > > I am using the following intent to invoke the camera: > > > > > Intent cameraIntent = new > > > Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); > > > startActivity(cameraIntent); > > > > > The image is saved into the sdcard with the time/date as the name. I > > > want to do a HTTPPost of this image once the picture is taken. > > > > > How do I get the name of the picture stored? Or how to I change the > > > name of the image stored? > > > > > Thanks In Advance, > > > Perumal > > > > > -- > > > 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 > > > > -- > > K Siva Sankar > > -- > 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 > -- K Siva Sankar -- 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
[android-developers] Re: HTTP Post Json to IIS Service
So it looks like your trying to connect to a server What remote web services are you using. For a scanner project I created last semester, I used XAMPP locally(just to see code work correctly), then I used Teamviewer when I went live. On the remote server file, I placed a php file with the JSON object to reference in the android code. Your code looks fine, however, I've learn it's not just this code that you may need. I connected my code using a button called my SQL... I'll post the code below... hope this helps... Also, something I didn't find out until the end, but make sure your server PHP and code are up to date...I ran into a last minute issue I had to correct on the server side... This is the button code (click button, sends data to mySQL server from anywhere) case R.id.buttonSQL1: try { JSONObject json = new JSONObject(); json.put("barcode", mbarcodeEdit.getText()); json.put("description", description.getText()); json.put("make", make.getText()); json.put("serial", serial.getText()); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); String url = "http://10.0.2.2:8080/webservice4.php";; // needed for the php file pass over HttpPost request = new HttpPost(url); request.setEntity(new ByteArrayEntity(json.toString().getBytes( "UTF8"))); request.setHeader("json", json.toString()); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need if (entity != null) { InputStream instream = entity.getContent(); String result = RestClient.convertStreamToString(instream); Log.i("Read from server", result); Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } } catch (Throwable t) { Toast.makeText(this, "Request failed: " + t.toString(), Toast.LENGTH_LONG).show(); } // showDialog(DIALOG_ID); break; } } Also you need to insert this code (a separate java file called RestClient) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class RestClient { public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } public static void connect(String url) { HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response; try { response = httpclient.execute(httpget); // Examine the response status Log.i("Praeda",response.getStatusLine().toString()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); Stri
[android-developers] Re: HTTP post request with HTTP Auth
This is the way that i used for autnenticating a user in a Domino Server, hope will help... String namesUrl = "http://..."; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(namesUrl); List nameValuePairs = new ArrayList(2); //nameValuePairs.add(new BasicNameValuePair("RedirectTo", redirect)); nameValuePairs.add(new BasicNameValuePair("Username", "Username")); nameValuePairs.add(new BasicNameValuePair("Password", "Password")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); Before using this authentication i've tried many different one, without found one that worked, for me... Bye On 26 Nov, 01:54, Seb wrote: > As the topic indicates I'm trying to perform a http post request to a page > secured with basic http authentication. > > It works from the command line with curl as following: > curlhttp://snot%40snot.dk:supersnot_at_teletracker.herokuapp.com/devices > -d "device[name]=snot" -d "device[device_id]=1234" > > But with the java code below I get redirected to a login form. Any ideas > what I'm doing wrong? > Note that the username and password I have supplied is working so, if > anyone cares, please try it out. You just have to replace _at_ with @ > > private void sendDeviceIdToServer(String deviceId) { > DefaultHttpClient client = new DefaultHttpClient(); > client.getCredentialsProvider().setCredentials( > new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), > new UsernamePasswordCredentials("snot_at_snot.dk", "supersnot")); > //new UsernamePasswordCredentials("snot%40snot%2Edk", "supersnot")); > HttpPost post = new > HttpPost("http://teletracker.herokuapp.com/devices";); > try { > List nameValuePairs = new ArrayList(1); > nameValuePairs.add(new BasicNameValuePair("device[name]", "from > android")); > nameValuePairs.add(new BasicNameValuePair("device[device_id]", > deviceId)); > > post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); > > ResponseHandler handler = new BasicResponseHandler(); > String response = client.execute(post, handler); > Log.d("HttpResponse", response); > } catch (IOException e) { > e.printStackTrace(); > } > } Se hai ricevuto questa comunicazione per errore, ti preghiamo di non inoltrarla a nessun'altra persona in quanto potrebbe contenere informazioni riservate o privilegiate. Cancellane tutte le copie e informa il mittente che il messaggio è stato inviato per errore ad un'altra persona. Grazie. This email may be confidential or privileged. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it went to the wrong person. Thanks. I termini che precedono rappresentano un potenziale accordo commerciale e costituiscono esclusivamente una base per ulteriori negoziazioni, di conseguenza non danno origine ad obbligazioni legalmente vincolanti né potranno essere interpretati come tali. Nessuna obbligazione legalmente vincolante potrà sorgere o essere ritenuta implicitamente sorta fino a quando tutte le parti coinvolte non abbiano concluso e sottoscritto un accordo definitivo. -- 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
[android-developers] Re: Http Post failing on only one phone
ga...@deanblakely.com wrote: > The code pasted below has been successfully tested on several phones > running Gingerbread, ICS, and Jelly Bean. However when testing on a > friends Samsung 4GLTE model SCH-1200 (verison) the code fails. > The post throws an exception and in the Catch, the log throws a null > Pointer exception [sic]. > Where? What exception? What is the exact (copied and pasted!) message? The log call actually shouldn't throw any exception. But since you don't show us the exception message it's really, really hard to diagnose your situation. > Log.w(getClass().getName(), e.getMessage().toString()); <---THIS GETS NULLPOINTER EXCEPTOION [sic] *What* "gets" a NullPointerException? I see nothing in this code that can throw an NPE. 'e' is non-null because it's in the 'catch' (and you shouldn't catch just 'Exception' anyway, usually). Perhaps the message is null, which is very rare but I suppose it can happen. Why do you call 'toString()' on a 'String'? You should get rid of that useless call. Give us data if you really want help. -- Lew -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
[android-developers] Re: HTTP post to non-standard port over Wifi?
Hi, Did you look at the router does it allow outgoing TCP traffic to flow through for port 8080? -Vinay On May 3, 3:48 pm, Per wrote: > Hi, > > I have trouble getting http post to work over WiFi. > > The server listens at port 8080, and posting works just fine when > disabling WiFi and just using 3G. > When enabling WiFi, I always get an IOException with message 'Socket > is not connected'. > > Using the standard port 80 (or opening raw sockets to other ports) > works just fine, but the combination (http + 8080 + WiFi) seems to > fail. > > I've been looking for hints in the android.net package, but > unsuccessfully. Any hints are welcome.. > > /Per > > -- > 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 > athttp://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
[android-developers] Re: HTTP post to non-standard port over Wifi?
(blushing) arrgh - should have thought of that. You're right. Wifi access to that ip:port does not work from a PC, either. Sorry for taking up anyone's bandwidth :) /Per On 4 Maj, 07:49, Vinay S wrote: > Hi, > > Did you look at the router does it allow outgoing TCP traffic to flow > through for port 8080? > > -Vinay > > On May 3, 3:48 pm, Per wrote: > > > > > Hi, > > > I have trouble getting http post to work over WiFi. > > > The server listens at port 8080, and posting works just fine when > > disabling WiFi and just using 3G. > > When enabling WiFi, I always get an IOException with message 'Socket > > is not connected'. > > > Using the standard port 80 (or opening raw sockets to other ports) > > works just fine, but the combination (http + 8080 + WiFi) seems to > > fail. > > > I've been looking for hints in the android.net package, but > > unsuccessfully. Any hints are welcome.. > > > /Per > > > -- > > 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 > > athttp://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 > athttp://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
[android-developers] Re: HTTP Post works in activity but not in service
Try putting it in onStartCommand() instead. I haven't had any problems doing posts from the service, but they all happen after the onCreate method. On Nov 5, 8:38 pm, Burk Ferden wrote: > Hello, > I am trying to do a http post to a php script. the method works if I > place it in the onCreate event of the application but gets a > ConnectionTimeOut error when it tries to execute it from the service. > Here is an example of my method > > String url = "http://myurl/"; + > "?tmpl=javascript" + > "&mode=recieved" + > "&id=" + id; > URI uri = null; > try { > url = url.replace(" ", "%20"); > uri = new URI(url); > } catch (URISyntaxException e1) { > // TODO Auto-generated catch block > e1.printStackTrace(); > } > HttpGet httpGet = new HttpGet(uri); > HttpParams httpParameters = new BasicHttpParams(); > // Set the timeout in milliseconds until a connection is > established. > int timeoutConnection = 1; > HttpConnectionParams.setConnectionTimeout(httpParameters, > timeoutConnection); > // Set the default socket timeout (SO_TIMEOUT) > // in milliseconds which is the timeout for waiting for data. > int timeoutSocket = 1; > HttpConnectionParams.setSoTimeout(httpParameters, > timeoutSocket); > > DefaultHttpClient httpClient = new > DefaultHttpClient(httpParameters); > try { > HttpResponse response = httpClient.execute(httpGet); > } catch (ClientProtocolException e) { > System.out.println(e.getLocalizedMessage()); > e.printStackTrace(); > } catch (IOException e) { > System.out.println(e.getLocalizedMessage()); > e.printStackTrace(); > } > > I have no idea why the service would get a time out error but the > application activity would not. I am baffled. -- 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
[android-developers] Re: HTTP Post works in activity but not in service
Indeed, the onCreate method of any component, including the Application, is the wrong place to do any network operation or other thing that can cause delay. In fact, you don't want to be doing it in any method that runs on the main thread, including onStartCommand. I'd suggest using an AsyncTask. Anything that needs to happen on the main thread after that, put in onPostExecute(). The URIException catch block allows the code to continue while it's in a bad state. I'd suggest instead rethrowing as an Error, e.g.: } catch (URIException e) { throw new Error(e); } The ClientProtocolException should possibly be rethrowing as well, so it can be handled by whatever means you have in place for informing the user that your app just crashed. The IOException is something that can happen in normal use, so it shouldn't be regarded as a crash, and generally, should not even be logged or printed, except in debugging. Whether it should just be caught and ignored, or rethrown and handled elsewhere, depends on the consequence to the user of a failed operation. It can retry, or it can update a little status indicator light to indicate you're not connected, or bring up a dialog box warning him, depending what this operation means to the user. On Nov 8, 10:56 am, loweroctave wrote: > Try putting it in onStartCommand() instead. I haven't had any > problems doing posts from the service, but they all happen after the > onCreate method. > > On Nov 5, 8:38 pm, Burk Ferden wrote: > > > > > > > > > Hello, > > I am trying to do a http post to a php script. the method works if I > > place it in the onCreate event of the application but gets a > > ConnectionTimeOut error when it tries to execute it from the service. > > Here is an example of my method > > > String url = "http://myurl/"; + > > "?tmpl=javascript" + > > "&mode=recieved" + > > "&id=" + id; > > URI uri = null; > > try { > > url = url.replace(" ", "%20"); > > uri = new URI(url); > > } catch (URISyntaxException e1) { > > // TODO Auto-generated catch block > > e1.printStackTrace(); > > } > > HttpGet httpGet = new HttpGet(uri); > > HttpParams httpParameters = new BasicHttpParams(); > > // Set the timeout in milliseconds until a connection is > > established. > > int timeoutConnection = 1; > > HttpConnectionParams.setConnectionTimeout(httpParameters, > > timeoutConnection); > > // Set the default socket timeout (SO_TIMEOUT) > > // in milliseconds which is the timeout for waiting for > > data. > > int timeoutSocket = 1; > > HttpConnectionParams.setSoTimeout(httpParameters, > > timeoutSocket); > > > DefaultHttpClient httpClient = new > > DefaultHttpClient(httpParameters); > > try { > > HttpResponse response = httpClient.execute(httpGet); > > } catch (ClientProtocolException e) { > > System.out.println(e.getLocalizedMessage()); > > e.printStackTrace(); > > } catch (IOException e) { > > System.out.println(e.getLocalizedMessage()); > > e.printStackTrace(); > > } > > > I have no idea why the service would get a time out error but the > > application activity would not. I am baffled. -- 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
[android-developers] Re: HTTP POST to Web Server running on Android Device
Kiran, I think you are looking for this: http://bytes.com/topic/java/answers/720825-how-build-http-post-request-java Cheers On Feb 27, 10:57 pm, Kiran wrote: > Hi All, > > I am trying to send a HTTP POST command from Android Device A to > Android Device B. Device B is running a simple web server. The code > that I am using for the webserver is > here:http://code.google.com/p/android-webserver/ > > I am adding functionality to the code for it to process HTTP POST > commands, currently it only supports HTTP GET. > > In order to send my HTTP POST command from Device A, I do the > following: > > HttpClient httpclient = new DefaultHttpClient(); > HttpPost httppost = new HttpPost(url); > > try { > List pairs = new ArrayList(); > pairs.add(new BasicNameValuePair("key1", "value1")); > pairs.add(new BasicNameValuePair("key2", "value2")); > httppost.setEntity(new UrlEncodedFormEntity(pairs)); > > HttpResponse httpresponse = httpclient.execute(httppost);} > > catch (IOException e) { > e.printStackTrace(); > > } > > In Device B, I have opened an input stream from a socket in Java. I > am printing out all the lines that I am receiving via the socket. The > code looks as follows: > > try { > in = new BufferedReader(new > InputStreamReader(toClient.getInputStream())); > > // Receive data > while (true) { > String s = in.readLine().trim(); > Log.i(TAG, "line=" + s); > if(s==null) { > break; > } > } > > } > > When I run the webserver on Device B and run the snippet of code > posted above on Device A, the only output of the webserver that I get > is: > line=POST / HTTP/1.1 > line=Content-Length: 23 > > I am wondering where the HTTP POST data is? Shouldn't I be able to > see it in the printout? It seems like I am only seeing the HTTP > header. > > Thanks in advance for your help, > K -- 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
[android-developers] Re: HTTP POST to Web Server running on Android Device
> line=POST / HTTP/1.1 > line=Content-Length: 23 No you would see: line=POST / HTTP/1.1 line=Content-Length: 23 line=0 if a null was returned. But a null is never returned. Still your loop ends as instead an exception is trown. You did not see that because the catch does nothing. Add a Log.i there and you will see. You modified the original code which checked for an empty line. Take the original code again. Add a check for the Content-Lengh: line and decode the contentlength. (in your example 23) Then outside the loop do something like: [code] if ( contentlength > 0 ){ char [] buffer = new char[contentlength]; int nread = in.read (buffer, 0, contentlength); String Parameters = new String( buffer); // well you should check nread == contentlength } [/code] > I am wondering where the HTTP POST data is? That just follows the empty line. But as that data is not ended with a newline in.readLine() cannot be used. -- 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
[android-developers] Re: HTTP POST to Web Server running on Android Device
> line=POST / HTTP/1.1 > line=Content-Length: 23 No, you would see: line=POST / HTTP/1.1 line=Content-Length: 23 line=0 if a null was returned. But a null is never returned. Still your loop ends as instead an exception is trown which you did not see because the catch statement is empty. Add a Log.i there and you will see. You modified the original code where the loops ends at the first empty line. Take the original code again en check for the Content-Length line. Parse out the value for contentlength; After the loop add following code [code] if ( contentlength > 0 ) { char [] buffer = new char[contentlength]; int nread = in.read (buffer, 0, contentlength); String Parameters = new String( buffer); // well you should check if nread equals contentlength } [/code] > I am wondering where the HTTP POST data is? That just follows the empty line. But as this post data is not terminated with a newline character in.readLine() cannot be used. -- 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
[android-developers] Re: HTTP POST to Web Server running on Android Device
> line=POST / HTTP/1.1 > line=Content-Length: 23 No, you would see: line=POST / HTTP/1.1 line=Content-Length: 23 line=0 if a null was returned. But a null is never returned. Still your loop ends as instead an exception is trown which you did not see because the catch statement is empty. Add a Log.i there and you will see. You modified the original code where the loops ends at the first empty line. Take the original code again en check for the Content-Length line. Parse out the value for contentlength; After the loop add following code if ( contentlength > 0 ) { char [] buffer = new char[contentlength]; int nread = in.read (buffer, 0, contentlength); String Parameters = new String( buffer); // well you should check if nread equals contentlength } > I am wondering where the HTTP POST data is? That just follows the empty line. But as this post data is not terminated with a newline character in.readLine() cannot be used. -- 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] Re: HTTP Post works in activity but not in service
On Mon, Nov 8, 2010 at 8:56 PM, loweroctave wrote: > Try putting it in onStartCommand() instead. I haven't had any > problems doing posts from the service, but they all happen after the > onCreate method. > > On Nov 5, 8:38 pm, Burk Ferden wrote: > > Hello, > > I am trying to do a http post to a php script. the method works if I > > place it in the onCreate event of the application but gets a > > ConnectionTimeOut error when it tries to execute it from the service. > > Here is an example of my method > > > > String url = "http://myurl/"; + > > "?tmpl=javascript" + > > "&mode=recieved" + > > "&id=" + id; > > URI uri = null; > > try { > > url = url.replace(" ", "%20"); > > uri = new URI(url); > > } catch (URISyntaxException e1) { > > // TODO Auto-generated catch block > > e1.printStackTrace(); > > } > > HttpGet httpGet = new HttpGet(uri); > > HttpParams httpParameters = new BasicHttpParams(); > > // Set the timeout in milliseconds until a connection is > > established. > > int timeoutConnection = 1; > > HttpConnectionParams.setConnectionTimeout(httpParameters, > > timeoutConnection); > > // Set the default socket timeout (SO_TIMEOUT) > > // in milliseconds which is the timeout for waiting for > data. > > int timeoutSocket = 1; > > HttpConnectionParams.setSoTimeout(httpParameters, > timeoutSocket); > > > > DefaultHttpClient httpClient = new > > DefaultHttpClient(httpParameters); > > try { > > HttpResponse response = > httpClient.execute(httpGet); > > } catch (ClientProtocolException e) { > > System.out.println(e.getLocalizedMessage()); > > e.printStackTrace(); > > } catch (IOException e) { > > System.out.println(e.getLocalizedMessage()); > > e.printStackTrace(); > > } > > > > I have no idea why the service would get a time out error but the > > application activity would not. I am baffled. > > Please provide your LogCat > -- > 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 > -- tarek -- 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
[android-developers] Re: HTTP Post works only on port 80 when on mobile network (Galaxy Nexus)
Can someone please confirm me, that is a problem posting data on a server on non-80 port, using mobile data, on Galaxy Nexus or probably some other ICS device? If it works for you, how do you do it? Thanks. On Sunday, July 15, 2012 12:10:02 PM UTC+2, Zoran Smilevski wrote: > > Hello! > > I have a problem with posting data when using mobile data network on a > port different than 80. This happens only on Galaxy Nexus. I have also > reported a bug with detailed description ( > http://code.google.com/p/android/issues/detail?id=34769). There is also a > demo app so anyone can test this. > > I was also trying to send a post using a socket, like it's described on > http://www.exampledepot.com/egs/java.net/PostSocket.html. In this case > everything works fine. > > I hope that someone will see this and tell me what the hell am I doing > wrong or confirm that bug :) > -- 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
[android-developers] Re: HTTP Post works only on port 80 when on mobile network (Galaxy Nexus)
Works fine using 3g on my Galaxy Nexus (stock, Android 4.1.1). Are you testing using the same carrier and APN settings on the Desire and Galaxy Nexus? I've had some issues with a bad carrier proxy that garbled non-port 80 traffic, fixed by removing the proxy from APN. On Sunday, July 15, 2012 12:10:02 PM UTC+2, Zoran Smilevski wrote: > > Hello! > > I have a problem with posting data when using mobile data network on a > port different than 80. This happens only on Galaxy Nexus. I have also > reported a bug with detailed description ( > http://code.google.com/p/android/issues/detail?id=34769). There is also a > demo app so anyone can test this. > > I was also trying to send a post using a socket, like it's described on > http://www.exampledepot.com/egs/java.net/PostSocket.html. In this case > everything works fine. > > I hope that someone will see this and tell me what the hell am I doing > wrong or confirm that bug :) > -- 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
[android-developers] Re: HTTP Post works only on port 80 when on mobile network (Galaxy Nexus)
Oh yes, thanks. I don't see on Desire APN settings, but when I removed a proxy from APN on Nexus, response got through. Thanks again, you saved me :) On Monday, July 23, 2012 9:45:53 AM UTC+2, Johan Appelgren wrote: > > Works fine using 3g on my Galaxy Nexus (stock, Android 4.1.1). > > Are you testing using the same carrier and APN settings on the Desire and > Galaxy Nexus? I've had some issues with a bad carrier proxy that garbled > non-port 80 traffic, fixed by removing the proxy from APN. > > On Sunday, July 15, 2012 12:10:02 PM UTC+2, Zoran Smilevski wrote: >> >> Hello! >> >> I have a problem with posting data when using mobile data network on a >> port different than 80. This happens only on Galaxy Nexus. I have also >> reported a bug with detailed description ( >> http://code.google.com/p/android/issues/detail?id=34769). There is also >> a demo app so anyone can test this. >> >> I was also trying to send a post using a socket, like it's described on >> http://www.exampledepot.com/egs/java.net/PostSocket.html. In this case >> everything works fine. >> >> I hope that someone will see this and tell me what the hell am I doing >> wrong or confirm that bug :) >> > -- 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
[android-developers] Re: HTTP Post works only on port 80 when on mobile network (Galaxy Nexus)
I have done even more research. Solution is to use Proxy.NO_PROXY: HttpURLConnection conn = (HttpURLConnection)mUrl.openConnection(Proxy. NO_PROXY); On Monday, July 23, 2012 10:02:02 AM UTC+2, Zoran Smilevski wrote: > > Oh yes, thanks. I don't see on Desire APN settings, but when I removed a > proxy from APN on Nexus, response got through. > > Thanks again, you saved me :) > > On Monday, July 23, 2012 9:45:53 AM UTC+2, Johan Appelgren wrote: >> >> Works fine using 3g on my Galaxy Nexus (stock, Android 4.1.1). >> >> Are you testing using the same carrier and APN settings on the Desire and >> Galaxy Nexus? I've had some issues with a bad carrier proxy that garbled >> non-port 80 traffic, fixed by removing the proxy from APN. >> >> On Sunday, July 15, 2012 12:10:02 PM UTC+2, Zoran Smilevski wrote: >>> >>> Hello! >>> >>> I have a problem with posting data when using mobile data network on a >>> port different than 80. This happens only on Galaxy Nexus. I have also >>> reported a bug with detailed description ( >>> http://code.google.com/p/android/issues/detail?id=34769). There is also >>> a demo app so anyone can test this. >>> >>> I was also trying to send a post using a socket, like it's described on >>> http://www.exampledepot.com/egs/java.net/PostSocket.html. In this case >>> everything works fine. >>> >>> I hope that someone will see this and tell me what the hell am I doing >>> wrong or confirm that bug :) >>> >> -- 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