Undefined index, android doesn't send data to php code -
i wrote program send json
android device server (xampp). tested php code using form , received data correctly.
my app, on other hand, sends no data server. executing var_dump($_post)
returns array(0)
on server side.
here's android code:
public background(context context){ this.context=context; } @override protected string doinbackground(string... params){ string location_url ="http://192.168.1.90/server_connection.php"; try{ url url1=new url(location_url); httpurlconnection httpurlconnection =(httpurlconnection)url1.openconnection(); httpurlconnection.setrequestmethod("post"); // httpurlconnection.setrequestproperty("content-type", "application/json"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); httpurlconnection.setrequestproperty("content-type", "application/json;charset=utf-8"); httpurlconnection.setrequestproperty("accept", "application/json"); outputstream stream_upload=httpurlconnection.getoutputstream(); bufferedwriter buffer_writer=new bufferedwriter(new outputstreamwriter(stream_upload,"utf-8")); string postdata= urlencoder.encode(string.valueof(params)); buffer_writer.write(postdata); buffer_writer.flush(); buffer_writer.close(); stream_upload.close(); int httpresult = httpurlconnection.getresponsecode(); if (httpresult == httpurlconnection.http_ok) { inputstream stream_dawnload = httpurlconnection.getinputstream(); bufferedreader bufferreader = new bufferedreader(new inputstreamreader(stream_dawnload, "iso-8859-1")); string result = ""; string line; while ((line = bufferreader.readline()) != null) { result += line; } bufferreader.close(); stream_upload.flush(); stream_dawnload.close(); httpurlconnection.disconnect(); return result; } }catch (exception e){ e.printstacktrace(); } return null; }
i send data code send_json
json
object:
background.execute(string.valueof(send_json));
i think problem caused json
object not being correctly inserted post
request body.
i know there questions similar issues none of solutions helped me.
i appreciate help.
there couple of points here should pay attention.
first, mentioned, seems problem because json
data not being formatted request. passing data seems asynctask
varargs
parameter: doinbackground(string... params).
transform string
object passed when calling execute(send_json)
string[]
object. calling string.valueof(params)
not return resembling data passed asynctask
text representation of string
array contains data.
second, i'd suggest use 3rd party library handle networking code you. code have write in order manage http
connection neither trivial nor concise. there better alternatives out there. okhttp great example , i'd highly encourage use it.
by way, i'm not discouraging knowing how create , manage http
connections in java/android. should try understand going on when send , receive data. i'm saying don't need handle complexity since there libraries job doing that.
Comments
Post a Comment