通过WRO服务通过ANDROID APP在SQLSERVER 2008中插入记录 [英] INSERT RECORD IN SQLSERVER 2008 By ANDROID APP Through WCF Service

查看:98
本文介绍了通过WRO服务通过ANDROID APP在SQLSERVER 2008中插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我是kuldeep singh我是android的新手。我的Android应用程序有问题。我想在sqlserver 2008 throgh WCF服务中插入记录。我的服务工作从Dotnet很好但它不适用于Android应用程序。我在Android中的Maniactivity.java代码

  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 活动{
私人 最终 static 字符串 SERVICE_URI = http:/ /125.16.63.234/WCFANDROID/Service.svc?wsdl;


@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final按钮=(按钮)findViewById(R.id.buttonsubmit);
// button.setOnClickListener(new View.OnClickListener(){
< span class =code-comment> //
public void onClick(查看v)
// {
// onSaveVehicleClick(查看)
// }
// });
}

@覆盖
public boolean onCreateOptionsMenu(菜单菜单){
// 给菜单充气;如果项目存在,这会将项目添加到操作栏。
getMenuInflater()。inflate(R.menu.activity_main,menu);
返回 true;
}

public void onSaveVehicleClick(查看视图)
{
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(), 您没有输入供应商名称,Toast.LENGTH_SHORT).show();
return ;
}
if (tlocation.getText()。toString()。equals( )){
Toast.makeText( this 您没有输入位置,Toast.LENGTH_SHORT).show();
return ;
}
if (tAddress.getText()。toString()。equals( )){
Toast.makeText( this 您没有输入地址,Toast.LENGTH_SHORT).show();
return ;
}
if (tvmobileno.getText()。toString()。equals( )){
Toast.makeText( this 您没有输入Mobile no,Toast.LENGTH_SHORT).show();
return ;
}
if (tvmobileno.getText()。toString()。length()!= 10){
Toast.makeText ( this 请输入正确的手机号,Toast.LENGTH_SHORT)。show();
return ;
}
// 对< service> / SaveVehicle $ b的POST请求$ b HttpPost request = new HttpPost(SERVICE_URI + / SaveVender);
request.setHeader( 接受 application / json);
request.setHeader( Content-type application / json);

// 构建JSON字符串
try
{

JSONStringer venderjson = new JSONStringer()
.object ()
.key( vname
.object()
.key( VName)。value(sUsername)
.key( Vlocation)。value(tvlocation)
.key( 地址)。value(stAddress)
.key( MobileNo)。value(Integer.pars eInt(stvmobileno.toString()))
.endObject()
.endObject();
StringEntity entity = new StringEntity(venderjson.toString());

request.setEntity(entity);

// 向WCF服务发送请求
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.v( 响应代码,response.getStatusLine()
.getStatusCode()+ );
Toast.makeText( this save记录成功,Toast.LENGTH_SHORT).show();
Toast.makeText( this save记录成功,Toast.LENGTH_SHORT).show();
Log.d( WebInvoke 保存: + response.getStatusLine()。getStatusCode());

}
catch (例外e)
{
e.printStackTrace();

} // 重新载入牌号



Plesae帮助



已添加代码块[/ Edit]

解决方案

< blockquote>我使用Odata4j,他们的网站上有关于如何为WCF服务做CRUD的例子。



https://code.google.com/p/odata4j /

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]

解决方案

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

https://code.google.com/p/odata4j/


这篇关于通过WRO服务通过ANDROID APP在SQLSERVER 2008中插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆