Hostname consist of hyphen is giving IllegalArgumentException in android - android

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");

Related

JsonArrayRequest, url parameter maximum size?

I'm new to android,
I'm using a Json webservice in my app to update some database fields, but I have an issue that I can't figure out.
With this one it's working :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"b\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Not working with this :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla bla bla\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Could the URL parameter size be the problem ?
Thanks a lot for your help.
Could be a problem with the spaces - try to use %20 instead of a space
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla%20bla%20bla\"}]";
Edit:
There actually is a character limit on GET parameters, but it is about 512, so it shouldn't be a problem here - but you should definitly think of a better solution for longer mails
(Source: Max size of URL parameters in _GET)

How to set character encoding of httpclient

I am using this httpclient: http://loopj.com/android-async-http/
I am getting a json with this httpclient.
I want to set character enconding of this httpclient. The JSONObject that the client returns contains turkish chars such as şğöü. But it is corrupted and i cant view this characters.
How can i set character encoding of this httpclient?
The correct would be that server provides the encoding of the returned page.
If it does that you will receive the correct one.
But if it doesn't provides the encoding Async-http seems to assume UTF-8 and looking at the code it doesn't seems to support providing a default alternative one.
Relevant code in AsyncHttpResponseHandler :
// Interface to AsyncHttpRequest
void sendResponseMessage(HttpResponse response) {
...
responseBody = EntityUtils.toString(entity, "UTF-8");
If you want to do you will need to user your own version of AsyncHttpResponseHandler or suggest a patch to be able to specify default encoding.
i resolved this problem by modifying the loopj source code file "AsyncHttpResponseHandler.java"...
void sendResponseMessage(HttpResponse response){
.........
//responseBody = EntityUtils.toString(entity, "UTF-8");
responseBody = EntityUtils.toString(entity, "ISO-8859-1");
}
ISO-8859-1 encoding will give you the correct characters..

Google-api-client for Multipart POST request

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.

Mystery URL decoding of certain characters - Android URI / Google App Engine

I am having a curious problem that perhaps someone has insight into. I encode a query string into a URL on Android using the following code:
request = REQUEST_BASE + "?action=loadauthor&author=" + URLEncoder.encode(author, "UTF-8");
I then add a few other parameters to the string and create a URI like this:
uri = new URI(request);
At a certain point, I pull out the query string to make a checksum:
uri.getRawQuery().getBytes();
Then I send it on its way with:
HttpGet get = new HttpGet(uri);
On the Appengine server, I then retrieve the string and try to match the checksum:
String query = req.getQueryString();
Normally, this works fine. However, there are a few characters that seem to get unencoded on the way to the server. For example,
action=loadauthor&author=Charles+Alexander+%28Ohiyesa%29+Eastman&timestamp=1343261225838&user=1479845600
shows up in the server logs (and in the GAE app) as:
action=loadauthor&author=Charles+Alexander+(Ohiyesa)+Eastman&timestamp=1343261226837&user=1479845600
This only happens to a few characters (like parentheses). Other characters remain encoded all the way through. Does anyone have a thought about what I might be doing wrong? Any feedback is appreciated.
I never did find a solution for this problem. I worked around it by unencoding certain characters on the client before sending things to the server:
request = request.replace("%28", "(");
request = request.replace("%29", ")");
request = request.replace("%27", "'");
If anyone has a better solution, I am sure that I (and others) would be interested!
URLEncoder does not encode parentheses and certain other characters, as they are supposed to be "safe" for most servers. See URLEncoder. You will have to replace these yourself if necessary.
Example:
URI uri = new URI(request.replace("(","%28"));
If a lot of replacements are needed, you can try request.replaceAll(String regularExpression, String replacement). This, of course, requires knowledge of regular expressions.

Size limit of a String while posting to server

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.

Categories

Resources