java - Consuming Restful WCF Service in Android -


i not sure causing request not execute. trying call wcf restful service in android, , receive error message "request error". looking @ example, don't see reason why example should not work. see below:

here .net service:

[servicecontract]     public interface isampleservice     {         [operationcontract]         [webinvoke(             method="post", uritemplate="/login", bodystyle= webmessagebodystyle.wrappedrequest, responseformat = webmessageformat.json, requestformat = webmessageformat.json)]         string login(string value);     }  public class sampleservice : isampleservice     {         public string login(string value)         {             string t = "";             try             {                 //foreach (string s in value)                 //{                 //    t = s;                 //}                 return t;             }             catch (exception e)             {                 return e.tostring();             }         }     } 

java:

package com.mitch.wcfwebserviceexample;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.entity.bytearrayentity; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicheader; import org.apache.http.message.basicnamevaluepair; import org.apache.http.params.httpconnectionparams; import org.apache.http.protocol.http; import org.json.jsonarray; import org.json.jsonobject; import org.json.jsonstringer;  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:8889";         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)           {                httpclient client = new defaulthttpclient();               httppost post = new httppost("http://10.0.2.2:65031/sampleservice.svc/login");               try               {                 list<namevaluepair> value = new arraylist<namevaluepair>(1);                 value.add(new basicnamevaluepair("value", "123456"));                 post.setentity(new urlencodedformentity(value));                 httpresponse response = client.execute(post) ;                 bufferedreader rd = new bufferedreader(new inputstreamreader(response.getentity().getcontent()));                 string line ="";                 while((line = rd.readline()) != null)                 {                     system.out.println(line);                 }               }catch(exception e)               {                   log.e("error", 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();         }     } } 

i got work way want to. issue was building array way (see below section 1) , pass jsonobject or jsonarray. switched , build array using jsonarray , pass jsonobject (see section 2). works charm.

section1: wrong way - (it may work way if through array , put them in jsonarray. it's work when can done directly.)

string[][] array = { new string[]{"example", "test"}, new string[]{"example", "test"}, };  jsonarray jar1 = new jsonarray(); jar1.put(0, array); 

// did not work section 2: way did after long hours of trying , helpful tips , hints @vorrtex.

**jsonarray jar1 = new jsonarray(); jar1.put(0, "abc"); jar1.put(1, "son"); jar1.put(2, "niece");**  **jsonarray jarr = new jsonarray(); jarr.put(0, jar1);**  jsonarray j = new jsonarray(); j.put(0,"session");  jsonobject obj = new jsonobject();           obj.put("value", jarr); obj.put("test", j); obj.put("name","myname"); log.d("obj.tostring message: ",obj.tostring()); stringentity entity = new stringentity(obj.tostring()); 

looking @ web service, , has looking for.

thanks help!!!!


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -