Help me about Webservice!

Put your problem here if it does not fit any of the other categories.

Help me about Webservice!

Postby bnkhanh » Thu Jan 03, 2008 6:02 pm

I have a project WebService (Server) written by C#, this code:


Syntax: [ Download ] [ Hide ]
Using csharp Syntax Highlighting
  1. //-----------------------------------------//
  2.  
  3. using System;
  4.  
  5. using System.Web;
  6.  
  7. using System.Web.Services;
  8.  
  9. using System.Web.Services.Protocols;
  10.  
  11.  
  12.  
  13. [WebService(Namespace = "http://tempuri.org/")]
  14.  
  15. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  16.  
  17. public class Service : System.Web.Services.WebService
  18.  
  19. {
  20.  
  21.     public Service () {        
  22.  
  23.     }      
  24.  
  25.  
  26.  
  27.     [WebMethod]
  28.  
  29.     public int Add(int a, int b){
  30.  
  31.         return a + b;
  32.  
  33.     }
  34.  
  35.    
  36.  
  37. }
  38.  
  39.  
  40.  
  41. //-----------------------//
  42.  
  43.  
  44.  
  45.  
  46.  
  47. And here is Client written by Android:
  48.  
  49.  
  50.  
  51.  
  52.  
  53. //-----------------//
  54.  
  55. public class WebserviceAdd extends Activity implements OnClickListener{
  56.  
  57.     /** Called when the activity is first created. */
  58.  
  59.     @Override
  60.  
  61.     public void onCreate(Bundle icicle) {
  62.  
  63.         super.onCreate(icicle);
  64.  
  65.         setContentView(R.layout.main);
  66.  
  67.         Button login=(Button)findViewById(R.id.btntinh);
  68.  
  69.         login.setOnClickListener(this);
  70.  
  71.     }
  72.  
  73.  
  74.  
  75.         public void onClick(View arg0) {
  76.  
  77.                
  78.  
  79.                 EditText value_a = (EditText)findViewById(R.id.txt_value_a);
  80.  
  81.                 EditText value_b = (EditText)findViewById(R.id.txt_value_b);
  82.  
  83.                 String a = value_a.getText().toString().trim();
  84.  
  85.                 String b = value_b.getText().toString().trim();                        
  86.  
  87.                
  88.  
  89.                   HttpClient client = new HttpClient();        
  90.  
  91.                   PostMethod get = new PostMethod("http://127.0.0.1:1496/WebService_Add_Sub_Mul_Div/   Service.asmx?op=Add");          
  92.  
  93.                   get.addRequestHeader("a", a);        
  94.  
  95.                   get.addRequestHeader("b", b);        
  96.  
  97.                  try {
  98.  
  99.                          int status = client.executeMethod(get);                       
  100.  
  101.                          EditText result = (EditText)findViewById(R.id.txtResult);
  102.  
  103.                          String res = "";
  104.  
  105.                          if(status!=404){
  106.  
  107.                                  res=get.getResponseBodyAsString();
  108.  
  109.                          }
  110.  
  111.                          else  
  112.  
  113.                                  res="Error";                  
  114.  
  115.                          result.setText(res);                    
  116.  
  117.                  }
  118.  
  119.                  catch(Exception ex){
  120.  
  121.                          showAlert("Error", ex.toString(), "ok", true);
  122.  
  123.                  }
  124.  
  125.                  finally {
  126.  
  127.                          get.releaseConnection();
  128.  
  129.                          get = null;
  130.  
  131.                  }                                     
  132.  
  133.         }
  134.  
  135. }
  136.  
  137.  
  138.  
  139. //-------------------------//
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


When i run Server(C#, WebService) and Client (Android), it show a alert "Java.IO.IOException: Connection refused". I don't know why. Please help me. Thanks
Attachments
WebService.rar
(37.13 KiB) Downloaded 161 times
Tran Van Khanh
Da Nang Polytechnic College
bnkhanh
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 07, 2007 8:03 am
Location: Viet Nam

Top

Postby cabernet1976 » Fri Jan 04, 2008 4:35 am

Hi,

Have you try to change 127.0.0.1 to your computer's IP address (i.e. 192.168.1.x)?
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby bnkhanh » Fri Jan 04, 2008 6:51 am

Hi cabernet1976.

I used with my IP or "localhost" but same result (error).
I know you worked with webservice, you can debug my error.
Tran Van Khanh
Da Nang Polytechnic College
bnkhanh
Freshman
Freshman
 
Posts: 5
Joined: Fri Dec 07, 2007 8:03 am
Location: Viet Nam

Postby cabernet1976 » Sun Jan 06, 2008 3:51 pm

Hi bnkhanh,
You can see also this thread in anddev.org: http://www.anddev.org/access_local_web_server-t435.html
Actually, I begin develope Java & web server until Google issue Android, so I am a novice in Java.
I suggest you can use ethereal(a tool from www.ethereal.com) to catch the ethernet packages, it is useful to debug a network program.

BTW. the server and client should be in different computers if you want to catch the package by ethereal.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby androidbeginner » Mon Mar 10, 2008 1:56 pm

hi...when i used the code above...i get a ClassCastException...can someone please help...

thanq..
androidbeginner
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Feb 05, 2008 7:10 am

Postby plusminus » Mon Mar 10, 2008 10:01 pm

ClassCastException :?:

which line...?

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby androidbeginner » Tue Mar 11, 2008 6:55 am

plusminus wrote:ClassCastException :?:

which line...?

Regards,
plusminus



how can i know that??
androidbeginner
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Feb 05, 2008 7:10 am

Postby androidbeginner » Tue Mar 11, 2008 7:37 am

i think the problem is with the get input given to executemethod()
executemethod requires a HttpMethod as input parameter whereas a postmethod object is passed on here...

is this the pblm..if so..any way out of this..

thanq
androidbeginner
Junior Developer
Junior Developer
 
Posts: 17
Joined: Tue Feb 05, 2008 7:10 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests