java - Sending Parameters to .net Restful WebService from Android -
i trying send invoke webservice method took jagged array parameters. build array, passed null web service.
here java class:
package com.mitch.wcfwebserviceexample; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicheader; import org.apache.http.protocol.http; import org.json.jsonarray; import org.json.jsonobject; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.app.activity; public class mainactivity extends activity implements onclicklistener { private string values =""; button btn; textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btn = (button)this.findviewbyid(r.id.btnaccess); tv = (textview)this.findviewbyid(r.id.tvaccess); btn.setonclicklistener(this); } @override public void onclick(view arg0) { try { asynctaskexample task = new asynctaskexample(this); task.execute(""); string test = values; tv.settext(values); } catch(exception e) { log.e("click exception ", e.getmessage()); } } public class asynctaskexample extends asynctask<string, void,string> { private string result=""; //private final static string service_uri = "http://10.0.2.2:1736"; private final static string service_uri = "http://10.0.2.2:65031/sampleservice.svc"; private mainactivity host; public asynctaskexample(mainactivity host) { this.host = host; } public string getsession(string url) { boolean isvalid = true; if(isvalid) { string[][] val = { new string[] {"student.id","123456"}, new string[] {"student.username","user1"}, new string[] {"student.password","123456"}, new string[] {"student.location.id","12"} }; httppost requestauth = new httppost(url +"/login"); try { jsonobject json = new jsonobject(); // json.put("sessionid", sessionid); jsonarray params = new jsonarray(); params.put(val); json.put("authenparams", params); stringentity se = new stringentity(json.tostring()); se.setcontenttype(new basicheader(http.content_type, "application/json")); requestauth.setheader("accept","application/json"); requestauth.setentity(se); defaulthttpclient httpclientauth = new defaulthttpclient(); httpresponse responseauth = httpclientauth.execute(requestauth); httpentity responseentityauth = responseauth.getentity(); char[] bufferauth = new char[(int)responseentityauth.getcontentlength()]; inputstream streamauth = responseentityauth.getcontent(); inputstreamreader readerauth = new inputstreamreader(streamauth); readerauth.read(bufferauth); streamauth.close(); string rawauthresult = new string(bufferauth); result = rawauthresult; string d = null; // } } catch (clientprotocolexception e) { log.e("client protocol", e.getmessage()); } catch (ioexception e) { log.e("client protocol", e.getmessage() ); } catch(exception e) { log.e("client protocol", e.getmessage() ); } } return result; } @override protected string doinbackground(string... arg0) { android.os.debug.waitfordebugger(); string t = getsession(service_uri); return t; } @override protected void onpostexecute(string result) { // host.values = result; super.onpostexecute(result); } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); } @override protected void oncancelled() { // todo auto-generated method stub super.oncancelled(); } } }
below method supposed recieve parameter: put break point in code below , check it. parameter null.
public string login(string[][] value) { string[] tester = null; string testerexample=""; foreach (string[] st in value) { tester = st; } foreach (string dt in tester) { testerexample = dt; } return testerexample; }
here method declaration in istudentservice:
[operationcontract] [webinvoke( method="post", uritemplate="login", bodystyle= webmessagebodystyle.wrappedrequest, responseformat = webmessageformat.json, requestformat = webmessageformat.json)] string login(string[][] value);
i tried suggested, , did not work. return "request error" here sample code paste.
httpclient client = new defaulthttpclient(); httppost post = new httppost("http://10.0.2.2:65031/sampleservice.svc/login"); list<namevaluepair> pairs = new arraylist<namevaluepair>(); pairs.add(new basicnamevaluepair("tester","abcd")); pairs.add(new basicnamevaluepair("sampletest","1234")); urlencodedformentity entity = new urlencodedformentity(pairs,http.utf_8); post.setentity(entity); httpresponse response = client.execute(post); httpentity responseentity = response.getentity(); char[] buffer = new char[(int)responseentity.getcontentlength()]; inputstream stream = responseentity.getcontent(); inputstreamreader reader = new inputstreamreader(stream); reader.read(buffer); stream.close(); string value = new string(buffer);
i changed web service contract xml, , urlencodedformentity still not work.
public interface sampleservice { [operationcontract] [webinvoke( method="post", uritemplate="/login", responseformat = webmessageformat.xml, requestformat = webmessageformat.xml)] string login(list<string> value); }
Comments
Post a Comment