I am posting a string to server. If string size is up to 6000KB then its posted successfully. But when size exceeded more than this its showing response -1.
I have tried method of posting: syn_data1 is string . records fetch from data base and then appending to A string builder and finally i create synData1 string from String builder
URL url = new URL(syn_data1);
URLConnection urlc = url.openConnection();
HttpURLConnection huc = (HttpURLConnection)urlc;
huc.setRequestMethod("POST");
huc.setConnectTimeout(3000);
huc.connect();
int response = huc.getResponseCode();
I do care about each special character and remove.But I did not get success
In theory, the URI in an HTTP request can be of any length, but the practical limit is on the order of 2k. Please read here for more info on that.
I am assuming the length is coming from the query string parameters (those name=value pairs that come after the ?). You should be putting these in the POST data, leaving the path part of the URI only. Of course, the server will have to be looking for those parameters in the POST data as well.
Are you passing the NameValue pairs properly . This is one successful way which i use .
List<NameValuePair> loginParams = new ArrayList<NameValuePair>(1);
loginParams.add(new BasicNameValuePair("ColumnName In DB",YourString));
then you do
httppost.setEntity(new UrlEncodedFormEntity(loginParams));
and proceed to execute
It's not clear exactly what you're trying to achieve, but this definitely looks wrong:
URL url = new URL(syn_data1.toString());
URLEncoder.encode(syn_data1.toString(),"UTF-16BE");
If syn_data1 is already a string, you don't need to call toString on it.. and calling URLEncoder.encode doesn't have any side-effects, so the second statement is pointless. Perhaps you want:
URL url = new URL(URLEncoder.encode(syn_data1, "UTF-16BE"));
That's just on the encoding side though - you still shouldn't be trying to use enormous URLs. If you have a lot of data, that should be in the body of the request rather than the URL.
Related
Here is my web service code
I can call it in my browser using http://sheltered-taiga-3258.herokuapp.com/toi/<input parameters> I am collecting Input parameters from user on android device. Obviously web service returns a JSON data which I need to display at client side in android application. I went through many posts and tutorials on android and web service but was not successful as many have the web service example of POST request and service in PHP. I want to do it for GET and service is in flask.
Please help Thank you.
EDIT:
I am calling web service using HttpGet object and I am passing my URL as parameter to it.
HttpGet httpget = new HttpGet(myURL);
and I am Constructing myURL as
EditText inputString = (EditText) findViewById(R.id.serverText);
String appendString =URLEncoder.encode(inputString.getText().toString(), "UTF-8") ;
private final HttpClient Client = new DefaultHttpClient();
String myURL = "http://sheltered-taiga-3258.herokuapp.com/toi/" + appendString;
Here I am getting myURL as
http://sheltered-taiga-3258.herokuapp.com/toi/hc+stays+toll+collection but I want it in this manner
http://sheltered-taiga-3258.herokuapp.com/toi/HC%20stays%20toll%20collection%20in%20kolhapur%20city
I know there is some url encoding problem but dont know way out of it.
Here is what gave me solution to my question may not be the suggestible and standard way of doing it but got me out of the problem:
String appendString = (inputString.getText().toString()).replace(" ", "%20") ;//here %20 is encoding for space
String myURL = "http://sheltered-taiga-3258.herokuapp.com/toi/" + appendString;
That gave me this :
http://sheltered-taiga-3258.herokuapp.com/toi/HC%20stays%20toll%20collection%20in%20kolhapur%20city
instead of this
http://sheltered-taiga-3258.herokuapp.com/toi/hc+stays+toll+collection
If I find the authentic way of doing this thing I will surely be posting it here marked as answer
I have an url like http://ashok-reddy:8080/hyd which consists hyphen. While making Http Post request, I am getting IllegalArgumentException saying Host name may not be null . I have tried with replacing the hyphen with its hexadecimal value and also tried converting using URLEncoder/Uri.encode(). But nothing has been worked till now.
mHttpPost = new HttpPost("ashok-reddy:8080/hyd");
mEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
mEnvelope.encodingStyle = SoapSerializationEnvelope.ENC;
mStringEntity = new StringEntity(soapData, HTTP.UTF_8);
mStringEntity.setContentType(mContext.getString(R.string.text_xml_content));
mHttpPost.setEntity(mStringEntity);
mHttpResponse = mHttpClient.execute(mHttpPost);
Can anyone please help on this?
Thanks in advance.
Arindam
If you are using a method like URLEncoder, you should not pass the full URL, because it will escape even '//' symbols in url. For example, it will encode :// into %3A%2F%2F
Pass to the function just the parameters list you need to encode to escape special characters.
EDIT:
As I can see you are using: mHttpPost = new HttpPost("ashok-reddy:8080/hyd");
instead of: mHttpPost = new HttpPost("http;//ashok-reddy:8080/hyd");
Has anyone released code to show the full HTTP request/response headers, any intermediate redirects, and any cookie data for the Android HttpURLConnection? This would be similar to Firefox Web Console
I roughly know how to write this myself, but 1) it's a non-trivial amount of code 2) it's tricky to get this kind of code to work in all instances. So i'm interested in finding a readymade solution. I know how to tcpdump the emulator, but I'm searching for code to print this information into the Android Log class for really quick runtime debugging.
for header fields
URL url = new URL(str_url);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.setConnectTimeout(TIMEOUT_SOCKET);
conection.setReadTimeout(TIMEOUT_CONNECTION);
conection.addRequestProperty("Accept-Encoding", "gzip");
RedirectLocations locations = new RedirectLocations();
// here u get all header fields and properties write it in logs
conection.getHeaderFields();
conection.getRequestProperties();
// conection.getOutputStream().write(buffer);
// download the file
InputStream is = conection.getInputStream();
// This is file path were a; quiz data will get saved.
// String file_path = context.getDir(folder,Activity.MODE_PRIVATE).getAbsolutePath();
return unzip(is,save_file_path);
for redirects
link
after u get response, again u ve to look for header fields
I use google-api-client for android. I try to do multipart POST request with text data and image file. Code snippet for creating request is below:
InputStream stream = new FileInputStream(fileToSend);
InputStreamContent photoContent = new InputStreamContent("image/jpeg", stream);
MultipartRelatedContent multiContent =
new MultipartRelatedContent(content, photoContent);
HttpRequest request = getRequestFactory().buildPostRequest(googleUrl, multiContent);
content is key-value text content. As a result I get error 500.
What I'm doing wrong?
There is a guide here about how to do media upload with the google-api-java-client here:
https://code.google.com/p/google-api-java-client/wiki/MediaUpload
That said, I don't anything necessarily wrong with your code either. It is possible that the googleUrl is incorrect, or that content is not properly formatted. You might want to try adding a URL query parameter uploadType=multipart to specify that you are using multipart as the protocol.
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).