2

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");
Marakalastic
  • 85
  • 12
  • Use name value pair , if you have to send an array of data. – Subin Thomas Sep 11 '15 at 18:28
  • Can't a name value pair only have 2 strings? Or are you implying I essentially make multiple writes to the server instead of writing a giant string to the server? – Marakalastic Sep 11 '15 at 18:39
  • From things I have read, NameValuePair accepts only 2 arguments, correct? Additionally, it is defined specifically in httpclient, right? I'm using HttpURLConnection and have 3 arguments. – Marakalastic Sep 11 '15 at 19:57
  • 1
    So in name value pair, set the argument like a joined string and explode at server.. I think you using it already.. But namevalue pair will be easy to handle. – Subin Thomas Sep 11 '15 at 20:08
  • Oh, I see what you're getting at! Just combine a string or two into the "name" or "value" part. I don't have access to the PHP side of things unfortunately though, I forgot to mention that. Wish you posted as an answer so I could give you the check mark! Additionally, I also found out that HttpURLConnection is only made to make one request so to make multiple requests, I put that specific connection in a while loop and read one line of my file each time. – Marakalastic Sep 11 '15 at 20:56
  • I posted a answer as per my understanding to your requirement. The refer links are mentioned in the answer. – Subin Thomas Sep 11 '15 at 21:54

1 Answers1

1

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.

Community
  • 1
  • 1
Subin Thomas
  • 1,408
  • 10
  • 19