how to upload the datas to webserver from android mobile.Please provide coding
I think this compiles:
HttpPut request = new HttpPut(<uri>);
request.setEntity(new ByteArrayEntity(<your data>));
HttpResponse response = HttpClient.execute(httpPut);
You might want to use the HttpPost instead of HttpPut and also specify the content type on the request.
Related
i am trying to create an application on android phone that takes username and password from user, encrypt the password using md5 then connect to url with these parameters.
a code to connect worked fine on iphone, but i couldn't find something like it in android:
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:myURL];
[request setHTTPMethod:#"GET"];
[request addValue:usernameField.text forHTTPHeaderField:#"UserName"];
[request addValue:MD5Pass2 forHTTPHeaderField:#"Password"];
i tried to connect via httpurlconnection used post/get Dataoutputstream, httpclient send parameters httpget/httppost, but no success.
I think I need to send the parameters as headerfield but I don't know how.
note: I compared encryption results and it was correct.
I find the Apache library to be much more straightforward when it comes to HTTP.
An example of this would be as follows:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.internet.com/api"));
Normally with a GET request you would use GET parameters, ie append them to the end of the URL like so:
String url = "http://www.internet.com/api?UserName=YourUsername&Password=yourpassword"
request.setURI(new URI(url));
But since you specified you want them as headers you could:
request.addHeader("UserName", username);
request.addHeader("Password", password);
and then:
HttpResponse response = client.execute(request);
//Parse the response from the input stream object inside the HttpResponse
you can try using HttpUrlConnection with this http://developer.android.com/reference/java/net/URLConnection.html#addRequestProperty%28java.lang.String,%20java.lang.String%29
I am trying to retrieve a JSON file from a web service using the following URL. That works fine when I use a browser to send the HTTP request.
For the Android application I came up with the following code.
// Android request
String url = "http://data.wien.gv.at/daten/geoserver/ows?service=WFS" +
"&request=GetFeature&version=1.1.0&typeName=ogdwien:BAUMOGD" +
"&srsName=EPSG:4326&outputFormat=json" +
"&bbox=16.377681,48.211448,16.379829,48.21341,EPSG:4326" +
"&maxfeatures=10"
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Though, EntityUtils does not output a JSON file but this XML exception.
// Value of result
<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/ows http://data.wien.gv.at/daten/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ows="http://www.opengis.net/ows">
<ows:Exception exceptionCode="NoApplicableCode">
<ows:ExceptionText>java.io.EOFException: input contained no data
input contained no data</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
I hope you can see what wents wrong ...
The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body. > [source]
Since you do encode the full request into the URL (request=GetFeature etc.) => use HttpGet instead.
Might even work imo with post since the url should still be transmitted to the server but the server would need to detect that the post request is actually a get request and behave accordingly.
How can I post a request using HttpPost from android to Asp.Net page?
I use such that
HttpPost request = new HttpPost(m_WebRequestUrl);
HttpParams params = new BasicHttpParams();
params.setParameter("Test", "Test");
request.setParams(params);
client.setParams(params);
response = client.execute(request);
Regards
Shibu
Whether Asp.Net or Php etc. doesnt matter .On server side you have to have a service that handles your request and responses in appropriate way.
Your request is good to go.
Greetings,
I'm developing an Android app and need to open a url (with POST parameters) over https and get the response.
There's the added complication that I have a self-signed certificate. I also need to accept cookies.
Anyone have any ideas about where to get started?
Many thanks in advance,
Android comes with the apache commons http library included.
Setting up a https post request is quite easy:
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
Android uses a version 4.x of the commons http library as all versions below 4.0 are out of their lifecycle.
I can't tell exactly how to register a self-signed certificate to the HttpClient, but mybe the commons http documentation helps:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
I managed to get it all working asyncronously with both cookies and unsigned https.
I used the code here:
http://masl.cis.gvsu.edu/2010/04/05/android-code-sample-asynchronous-http-connections/
and modified for unsigned https using Brian Yarger's code here:
Self-signed SSL acceptance on Android
(Add the above code to the beginning of run() in HttpConnection.java)
To get the cookies to work, I had to modify some code (POST snippet from HttpConnection.java):
case POST:
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(data));
httpPost.addHeader("Cookie", Cookie.getCookie());
response = httpClient.execute(httpPost);
Header[] headers=response.getAllHeaders();
for(int i=0;i<headers.length;i++){
if(headers[i].getName().equalsIgnoreCase("Set-Cookie")){
//Log.i("i",headers[i].getName()+"---"+headers[i].getValue());
Cookie.setCookie(headers[i].getValue());
break;
}
}
break;
Many thanks to everyone for pointing me in the direction,
Android supports a limited version of apache's http client(v4).
typically if I want to send binary data using content type= application/octet-stream via POST,
I do the following:
HttpClient client = getHttpClient();
HttpPost method=new HttpPost("http://192.168.0.1:8080/xxx");
System.err.println("send to server "+s);
if(compression){
byte[]compressed =compress(s);
RequestEntity entity = new ByteArrayRequestEntity(compressed);
method.setEntity(entity);
}
HttpResponse resp=client.execute(method);
however ByteArrayRequestEntity is not supported on android. what can I do?
I think you want ByteArrayEntity. ByteArrayRequestEntity is from 3.x