Click here to Skip to main content
15,614,138 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 in android

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.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 = "";
	   protected void onCreate(Bundle savedInstanceState) {
		//final Button button = (Button) findViewById(;
	   // button.setOnClickListener(new View.OnClickListener() {
            //public void onClick(View v) 
           // {
           // }

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(, menu);
		return true;
	public void onSaveVehicleClick(View view)
	    	EditText tvname = (EditText) findViewById(;
	        String sUsername =tvname.getText().toString() ;
	        EditText tlocation = (EditText) findViewById(;
	        String tvlocation = tlocation.getText().toString();
	        EditText tAddress = (EditText) findViewById(;
	        String stAddress = tAddress.getText().toString();
	        EditText tvmobileno = (EditText) findViewById(;
	        String stvmobileno = tvmobileno.getText().toString();
	        if (tvname.getText().toString().equals("")) 
	            Toast.makeText(getApplicationContext(), "You did not enter a Vender Name", Toast.LENGTH_SHORT).show();
	        if (tlocation.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Location", Toast.LENGTH_SHORT).show();
	        if (tAddress.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Address", Toast.LENGTH_SHORT).show();
	        if (tvmobileno.getText().toString().equals("")) {
	            Toast.makeText(this, "You did not enter a Mobile no", Toast.LENGTH_SHORT).show();
	        if (tvmobileno.getText().toString().length()!=10) {
	            Toast.makeText(this, "Please Enter Correct Mobile No", Toast.LENGTH_SHORT).show();
	        // 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
            JSONStringer venderjson = new JSONStringer()
            StringEntity entity = new StringEntity(venderjson.toString());
            // 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) 
   	    }  // Reload plate numbers

Plesae help

[Edit]Code block added[/Edit]
Updated 9-Mar-13 0:34am
Sandeep Mewara 9-Mar-13 6:42am    
What do you see when you debug?
kuldeep vatsal 9-Mar-13 6:49am    
its throw error on response.
HttpResponse response = httpClient.execute(request);
"the Source does not contain the for the file"
You can chan ge Source attchmnet by clicking source Below

1 solution

I use Odata4j and there are examples on their site on how to do CRUD to WCF service.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900