Post Json android in raw type -
i'm new on android programming. want ask how post json on android native. sample of json want post.
{ "user_id":"88880005", "keyword":"13828538", "items":["99999999999999999996","99999999999999999997"] } this have done.
private void save() { string url = url_manager.url_scanin; map<string, string> map = new hashmap<>(); map.put("user_id", id); map.put("keyword", keyword); map.put("items", databarcode); progressdialog progressdialog = new progressdialog(getactivity()); progressdialog.setmessage("loading"); progressdialog.setcancelable(false); try { aquery.progress(progressdialog).ajax(url, map, string.class, new ajaxcallback<string>() { @override public void callback(string url, string object, ajaxstatus status) { try { jsonobject jsonobject = new jsonobject(object); string result = jsonobject.getstring("success"); string message= jsonobject.getstring("message"); log.d("tag", jsonobject.tostring()); } catch (jsonexception e) { toast.maketext(getactivity(), e.getmessage, toast.length_long).show(); } } }); } catch (exception e) { toast.maketext(getactivity(), e.getmessage, toast.length_long).show(); } } in sample use postman in raw not form data. should post data android.
thank you.
follow these steps in android. add dependency in gradle
compile 'com.loopj.android:android-async-http:1.4.9' call webservice using below code
string[] items= new string[]{"99999999999","9999997456"}; jsonobject object=new jsonobject(); object.put("user_id",user_id); object.put("keyword",keyword); try { jsonarray jsonarray = new jsonarray(arrays.aslist(items)); object.put("items",jsonarray); }catch (exception e){} string yourdata = object.tostring(); stringentity entity = null; try { entity = new stringentity(yourdata); entity.setcontentencoding(new basicheader(http.content_type, "application/json")); } catch(exception e) { //exception } string url="your url here"; new asynchttpclient().post(null,url,entity,"application/json", new asynchttpresponsehandler() { @override public void onsuccess(int statuscode, cz.msebera.android.httpclient.header[] headers, byte[] responsebody) { try { string object= new string(responsebody); jsonobject jsonobject = new jsonobject(object); string result = jsonobject.getstring("success"); string message= jsonobject.getstring("message"); log.d("tag", jsonobject.tostring()); } catch (jsonexception e) { toast.maketext(getactivity(), e.getmessage, toast.length_long).show(); } } @override public void onfailure(int statuscode, cz.msebera.android.httpclient.header[] headers, byte[] responsebody, throwable error) { } }); }
Comments
Post a Comment