I'm writing an Android app that posts some CSV formatted data to a PHP server.
If I only post one row, the server reads it correctly and does what it needs to do. However, I'm unable to post multiple lines. I have the CSV data in a .txt file and I read this data into a string and send that string using HttpURLConnection. The issue is that while it'll look fine looking at the .txt file in my phone, the server does not interpret "\n" or "\r\n" correctly and won't update at all. My guess is that it interprets the entire string as one line or something along those lines.
Each line sent is read like this:
list($value1, $value2, $value3) = explode(",", $datarec);
Is there some sort of newline character that PHP can recognize? Apparently, it doesn't recognize \n or \r\n.
I currently have the content-type set to text/csv but I've also tried text/html and using br/> instead.
conn.setRequestProperty("Content-Type", "text/csv");
So here I prefer is to use NameValuePair. Each line, you combine to a string and create it as a NameValuePair, like
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("line1", "string1string2string3"));
And send it .
Refer to Add parameters to URLconnection and use of namevaluepair for more information.
Related
I'm trying to (HTTP) POST an image to a Nodejs server that is configured using Express. I have been able to accomplish this successfully using JSON, but unless I am mistaken, there is no way to obtain the image string without loading the entire request body into a new variable before parsing it as JSON. Since images are quite large and the image should already be stored in the request body anyway, is there a way to immediately pipe the image contents into fs.writeFile()? The content type for the request does not have to be JSON. I have tried using a querystring as well, but that was unsuccessful. The content type cannot be just an image though because I have to include a tag for the image too (in this case the user's email address).
Here is the code for when I attempted to use a query string. It is located in a post route method for the express app:
fs.writeFile('profiles/images/user.png', new Buffer(req.body.image, 'base64'),
function(error)
{
if (error)
res.end(error);
}
);
No error occurs, and the code creates the .png file, but the file is somehow corrupted and is larger than it should be.
All of this is actually for an Android app, so here is also the Java code that I am using to send the request:
URLConnection connection = new URL(UPLOAD_PICTURE_URL).openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;");
connection.setDoOutput(true);
String image = Base64.encodeToString(
IOUtils.toByteArray(new FileInputStream(filePath)),
Base64.NO_WRAP
);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("email=" + email + "&image=" + image);
out.close();
Perhaps this belongs in another topic, but along the same lines, does anybody know a way to pipe the file input stream in the android code directly to the URLConnection's output stream with base64 encoding? I have tried writing the string literal (the out.write() line above ^) and then creating a Base64OutputStream to write the image before piping that stream into the URLConnection's outputstream, but calling req.body.image in the node app after doing that just returns undefined. And finally, does anybody know if IOUtils.toByteArray() (from Apache Commons), when used as the input argument for an input/output stream constructor, writes the entire byte array to memory anyway on the Android side? If so, is there a way of avoiding that too?
Thanks in advance.
I programmed an Android App, which manages data to be stored and deleted in a MySQL database (on server). Whenever on the smartphone special characters ("ä", "ü", ...) are used the symbol is converted badly.
In the log I can see that the "Umlaut" (e.g. "ä") is transmitted properly. I also use in my php file "SET NAMES 'UTF-8'", see here:
function connect()
{
....
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_query("SET NAMES 'utf8'");
// Selecting database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
return $con;
}
If I start a request via Postman, I can add words with special characters, e.g. "TÄST", the json response looks as follows:
{"catId":"35","category":"T\u00c4ST"}
So the words are well converted to UTF-8. But if I add an Item via smartphone, the response from the server (to retrieve the added item) looks like this:
{"catId":"37","category":"T?ST"}
The position in my code, where I add the parameters for the JSON Object is that (note that "name" is the string content of the edit text field):
List<NameValuePair> params = new ArrayList<NameValuePair>();
...
params.add(new BasicNameValuePair("category", catName));
After that the HTTPRequest is send:
JSONObject json = jsonParser.makeHttpRequest(url_dest, "POST", params);
If I print out the params the word "Täst" is visible... But unfortunately I'm not able to check which json string is arriving on the server (due to my bad php knowledge). So where's the problem? In my android application or in the php files located on the server?
Do I have to encode the outgoing json object in any way?
Thanks a lot for your help.
To debug on Eclipse I suggest you:
http://projects.eclipse.org/projects/tools.pdt
It is quite easy to configure.
Control the encoding in the DB; maybe the VARCHAR default latin1_swedish_ci can't save that string.
Another way to solve it could be looking to the apache commons:
http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
in particular the functions provided by: escapeHtml4 and unescapeHtml4
#dafocus, if you set your Android app project to UTF-8 encoding in your IDE, it should ensure that UTF-8 is handled properly in the app. It looks like an encoding change somewhere. You might not need to convert TÄST to T\u00c4ST at all if the encoding is UTF-8 throughout. PHP used to be difficult with charsets. I would suggest you to look at the PHP to figure out if the handler page causes this (possible) and/or the DB - as #madx just said.
How can i configure RestTemplate (Springframework) to encode using percent-encoding rather characters encoding, for example i am posting this parameters to a server:
client_id=xxx
client_secret=xxx
grant_type=client_credentials
scope=public_read registration
but when posting, spring send it as:
client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=public_read+registration
and i want it to be like that:
client_id=xxx&client_secret=xxx&grant_type=client_credentials&scope=public_read%20registration
it converts spaces to + and i want it to be %20
thx
You can use this:
String formated_urlString = URLEncoder.encode(unformated_url_string, "utf-8").replace("+", "%20").
Also see
URLEncoder not able to translate space character
I've created a JSON string in a php file. I've then used json_encode($jsonStr) to encode the string.
$jsonStr =
"{
\"statusCode\": 0,
\"errorMsg\": \"SUCCESS\",
\"id\": $id,
\"message\": ".json_encode($message).",
\"author\": \"$author\",
\"showAfter\": \"$date\"
}";
I'm making a network call in java (Android) to get this string. My next step is to decode the string, however this doesn't seem to be working too well.
Here is a sample of what I'm trying to decode in my Android code:
{\n\t\t\t\t\"statusCode\": 0,\n\t\t\t\t\"errorMsg\": \"SUCCESS\",\n\t\t\t\t\"id\": 1,\n\t\t\t\t\"message\": \"This is a message.\",\n\t\t\t\t\"author\": \"Anonymous\",\n\t\t\t\t\"showAfter\": \"2013-06-18 01:19:49\"\n\t\t\t}
Yes it is riddled with encoded line breaks and such. I assumed that might be the issue so I took those out, however I still have issues, so I'm guessing there must be something bigger going on.
I know this is valid JSON because I'm able to decode it and use it in a javascript based website.
How can I accomplish this on Android/Java?
Your original JSON string (the one you show in your first snippet) looks to be valid JSON already. You must not encode it. Encoding it is what makes it invalid JSON, transforming every tab into \t, and every new line into \n.
Read the documentation of json_encode carefully.
I need help. In my current development one of the requirements says:
The server will return 200-OK as a response(httpresponse).
If the panelist is verified then as a result, the server must also
return the panelist id of this panelist.
The server will place the panelist id inside the body of the 200-OK
response in the following way:
<tdcp>
<cmd>
<ack cmd=”Init”>
<panelistid>3849303</panelistid>
</ack>
</cmd>
Now I am able to put the httpresponse as
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
And I can put
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
Now what does putting the above xml inside the body of 200-OK response mean and how can it be achieved?
You can write the XML directly to the response as follows:
This example uses a ServletResponse.getWriter(), which is a PrintWriter to write a String to the response.
String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write(responseToClient);
httpServletResponse.getWriter().flush();
You simply need to get the output stream (or output writer) of the servlet response, and write to that. See ServletResponse.getOutputStream() and ServletResponse.getWriter() for more details.
(Or simply read any servlet tutorial - without the ability to include data in response bodies, servlets would be pretty useless :)
If that's meant to be XML, Word has already spoiled things for you by changing the attribute quote symbol to ” instead of ".
It is worth having a look at JAXP if you want to generate XML using Java. Writing strings with < etc. in them won't scale and you'll run into problems with encodings of non-ASCII characters.