android - How can I POST "string request" and get data? -
i have tried application on android 23 , stuck somewhere.
i have request not url format!!!! string format. post on server , return data.
for example url is=http://api.someurl/app_dev.php/tr/content
and need post string parameter
{ "command":"read", "ctrl":"summaryofday", "data":{"date":"08.04.2016"}, "order":null, "limit":null }
and should return json data. because this request search parameter !
my code is
httpurlconnection connection=null; bufferedreader reader=null; try { url url=new url(params[0]); connection=(httpurlconnection)url.openconnection(); inputstream stream=connection.getinputstream(); reader=new bufferedreader(new inputstreamreader(stream)); stringbuffer buffer=new stringbuffer(); string line = ""; while ((line = reader.readline())!= null){ buffer.append(line); } string sjson = buffer.tostring(); jsonobject mjson = new jsonobject(sjson); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } return null;
so how can post these string , json date.
regards.
i have solved :)
searched many hours, tried many thinks , got :)
if have parameter json data example have weather apı , need somewhere weather.
this way explain how sending string request on api url , json data.
for example request is,
{ "command":"read", "ctrl":"summaryofday", "data":{"date":"08.04.2016"}, "order":null, "limit":null }
i need info data.
after code work.
private class downloadapi extends asynctask<string,string,string>{ string datestr; progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("akış bilgileri getiriliyor..."); pdialog.setindeterminate(true); pdialog.setcancelable(false); pdialog.show(); } @override protected string doinbackground(string... params) { httpurlconnection connection = null; bufferedreader reader = null; date timenow = new date(); dateformat df = new simpledateformat("dd.mm.yyyy"); datestr=df.format(timenow)+""; string data="{\"command\":\"read\",\"ctrl\":\"summaryofday\",\"data\":{\"date\":\""+datestr+"\"},\"order\":null,\"limit\":null}"; try { url url = new url(params[0]); connection = (httpurlconnection) url.openconnection(); connection.setdooutput(true); connection.setrequestproperty("content-type", "application/json"); connection.setrequestproperty("accept", "application/json"); connection.setrequestmethod("post"); connection.connect(); // still works without line, don't know why outputstream os = connection.getoutputstream(); bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8")); writer.write(data); writer.close(); os.close(); inputstream stream = connection.getinputstream(); reader = new bufferedreader(new inputstreamreader(stream)); stringbuffer buffer = new stringbuffer(); string line = ""; while ((line = reader.readline())!= null){ buffer.append(line); } string sjson = buffer.tostring(); jsonobject mjson = new jsonobject(sjson); return sjson; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } { if(connection != null) { connection.disconnect(); } if(reader != null){ try { reader.close(); } catch (ioexception e) { e.printstacktrace(); } } } return null; } @override protected void onpostexecute(string result) { super.onpostexecute(result); pdialog.dismiss(); mtext.settext(result.tostring()); } }
thanks helped me :)
Comments
Post a Comment