Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WCF Android SqlServerm
hi
i am kuldeep singh i am new on android. iahve a problem with android app. i wanna insert record in sqlserver 2008 throgh WCF Service. my service works fine fro Dotnet But it not working on android app. my code for Maniactivity.java in android
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
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.json.JSONException;
import org.json.JSONStringer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
	private final static String SERVICE_URI = "http://125.16.63.234/WCFANDROID/Service.svc?wsdl";
	
	   
	    @Override
	   protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//final Button button = (Button) findViewById(R.id.buttonsubmit);
	   // button.setOnClickListener(new View.OnClickListener() {
            //public void onClick(View v) 
           // {
            	//onSaveVehicleClick(view)
           // }
        //});
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	public void onSaveVehicleClick(View view)
	{ 
	    	EditText tvname = (EditText) findViewById(R.id.editTextVname);
	        String sUsername =tvname.getText().toString() ;
	        EditText tlocation = (EditText) findViewById(R.id.editTextloc);
	        String tvlocation = tlocation.getText().toString();
	        EditText tAddress = (EditText) findViewById(R.id.editTextadd);
	        String stAddress = tAddress.getText().toString();
	        EditText tvmobileno = (EditText) findViewById(R.id.editTextmobileno);
	        String stvmobileno = tvmobileno.getText().toString();
	        if (tvname.getText().toString().equals("")) 
	        {
	            Toast.makeText(getApplicationContext(), "You did not enter a Vender Name", Toast.LENGTH_SHORT).show();
	            return;
	        }
	        if (tlocation.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Location", Toast.LENGTH_SHORT).show();
	            return;
	        }
	        if (tAddress.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Address", Toast.LENGTH_SHORT).show();
	           	return;
	        }
	        if (tvmobileno.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Mobile no", Toast.LENGTH_SHORT).show();
	            return;
	        }
	        if (tvmobileno.getText().toString().length()!=10) {
	            Toast.makeText(this, "Please Enter Correct Mobile No", Toast.LENGTH_SHORT).show();
	            return;
	        }
	        // POST request to <service>/SaveVehicle
            HttpPost request = new HttpPost(SERVICE_URI + "/SaveVender");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
 
            // Build JSON string
           try
           {
       
            JSONStringer venderjson = new JSONStringer()
                .object()
                    .key("vname")
                        .object()
                            .key("VName").value(sUsername)
                            .key("Vlocation").value(tvlocation)
                            .key("Address").value(stAddress)
                            .key("MobileNo").value(Integer.parseInt(stvmobileno.toString()))
                        .endObject()
                    .endObject();
            StringEntity entity = new StringEntity(venderjson.toString());
 
            request.setEntity(entity);
 
            // Send request to WCF service
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            Log.v("response code", response.getStatusLine()
                    .getStatusCode() + ""); 
            Toast.makeText(this, "save record Succesfully", Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "save record Succesfully", Toast.LENGTH_SHORT).show();
            Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
             
           }
         catch (Exception e) 
   	    {
   	    	e.printStackTrace();
   	    	
   	    }  // Reload plate numbers
            
	        
	        
	      
Plesae help
 
[Edit]Code block added[/Edit]
Posted 9-Mar-13 1:29am
Edited 9-Mar-13 1:34am
ProgramFOX127.8K
v2
Comments
Sandeep Mewara at 9-Mar-13 6:42am
   
What do you see when you debug?
kuldeep1586 at 9-Mar-13 6:49am
   
its throw error on response.
HttpResponse response = httpClient.execute(request);
"the Source does not contain the for the file Activity.java"
You can chan ge Source attchmnet by clicking source Below

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I use Odata4j and there are examples on their site on how to do CRUD to WCF service.
 
https://code.google.com/p/odata4j/
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100