我已经为登录创建了wcf服务methot。
我想在android客户端中执行登录操作。但是当我以JSON格式将对象发布到WCF服务时,我得到一个Null对象(在Wrapped配置中)或者我得到一个字段为空的对象(在Bare配置中)。有人有解决方案吗?
这是我的Iservice1
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "login?name={name}&password={password}")]
string login(string name, string password);
这是我在Service1上的登录方法
public string login(string name, string password)
{
ads_dbDataContext db = new lidhaj_dbDataContext();
var messages = from p in db.tblUNI_WEB_Users
where p.Student== name && p.Password== password
select p;
if (messages.Count() > 0)
{
return "1";
}
else
{
return "0";
}
}
这是我在android的客户端
public class MainActivity extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
HttpGet httpget;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session=new SessionManager(getApplicationContext());
b = (Button)findViewById(R.id.btnLogin);
et = (EditText)findViewById(R.id.txtShfrytezuesi);
pass= (EditText)findViewById(R.id.txtFjakalimi);
tv = (TextView)findViewById(R.id.txterrorloginmessage);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost=new HttpPost("http://10.0.2.2/Service1.svc/login");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("response form wcf : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("1")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
// startActivity(new Intent(MainActivity.this, Njoftimet_e_studentit.class));
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
但这不起作用。 我在日志猫上收到此错误:
Exception : Not Found
这是我完整的日志猫:
12-16 16:22:55.854 1152-1152/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am 12-16 16:22:55.924 80-253/system_process I/ActivityManager﹕ START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.cmd.cmduniverziteti/.MainActivity} from pid 1152 12-16 16:22:55.964 80-253/system_process W/WindowManager﹕ Failure taking screenshot for (240x394) to layer 21005 12-16 16:22:56.064 1152-1152/? D/AndroidRuntime﹕ Shutting down VM 12-16 16:22:56.104 1163-1163/? D/dalvikvm﹕ Not late-enabling CheckJNI (already on) 12-16 16:22:56.114 1152-1165/? I/AndroidRuntime﹕ NOTE: attach of thread 'Binder Thread #3' failed 12-16 16:22:56.164 1152-1156/? D/dalvikvm﹕ GC_CONCURRENT freed 101K, 77% free 483K/2048K, paused 1ms+2ms 12-16 16:22:56.164 1152-1158/? D/jdwp﹕ Got wake-up signal, bailing out of select 12-16 16:22:56.164 1152-1158/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries 12-16 16:22:56.214 80-206/system_process I/ActivityManager﹕ Start proc com.cmd.cmduniverziteti for activity com.cmd.cmduniverziteti/.MainActivity: pid=1163 uid=10040 gids={3003} 12-16 16:22:56.414 80-108/system_process I/WindowManager﹕ createSurface Window{41b1aab8 Starting com.cmd.cmduniverziteti paused=false}: DRAW NOW PENDING 12-16 16:22:56.854 80-118/system_process W/NetworkManagementSocketTagger﹕ setKernelCountSet(10040, 1) failed with errno -2 12-16 16:22:59.294 80-222/system_process I/WindowManager﹕ createSurface Window{41a29c00 com.cmd.cmduniverziteti/com.cmd.cmduniverziteti.MainActivity paused=false}: DRAW NOW PENDING 12-16 16:22:59.604 80-118/system_process W/NetworkManagementSocketTagger﹕ setKernelCountSet(10005, 0) failed with errno -2 12-16 16:22:59.794 1163-1163/com.cmd.cmduniverziteti D/gralloc_goldfish﹕ Emulator without GPU emulation detected. 12-16 16:23:00.634 1163-1163/com.cmd.cmduniverziteti W/TextLayoutCache﹕ computeValuesWithHarfbuzz -- need to force to single run 12-16 16:23:00.722 80-80/system_process W/InputManagerService﹕ Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@41965210 (uid=10005 pid=177) 12-16 16:23:00.881 80-108/system_process I/ActivityManager﹕ Displayed com.cmd.cmduniverziteti/.MainActivity: +4s857ms 12-16 16:23:09.457 1163-1163/com.cmd.cmduniverziteti D/InputEventConsistencyVerifier﹕ KeyEvent: ACTION_UP but key was not down. in android.widget.EditText@4178fcf0 0: sent at 22117781000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=22117781, downTime=22117691, deviceId=0, source=0x101 } 12-16 16:23:19.274 1163-1166/com.cmd.cmduniverziteti D/dalvikvm﹕ GC_CONCURRENT freed 170K, 3% free 14295K/14599K, paused 11ms+23ms 12-16 16:23:20.484 133-133/com.android.systemui D/dalvikvm﹕ GC_FOR_ALLOC freed 863K, 35% free 15865K/24391K, paused 282ms 12-16 16:23:22.034 1163-1163/com.cmd.cmduniverziteti D/InputEventConsistencyVerifier﹕ KeyEvent: ACTION_UP but key was not down. in android.widget.EditText@4178fcf0 0: sent at 22130473000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=22130473, downTime=22130387, deviceId=0, source=0x101 } -- recent events -- 1: sent at 22125272000000, (unhandled) KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DEL, scanCode=14, metaState=0, flags=0x8, repeatCount=0, eventTime=22125272, downTime=22125211, deviceId=0, source=0x101 } 2: sent at 22125211000000, (unhandled) KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DEL, scanCode=14, metaState=0, flags=0x8, repeatCount=0, eventTime=22125211, downTime=22125211, deviceId=0, source=0x101 } 3: sent at 22123234000000, (unhandled) KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DEL, scanCode=14, metaState=0, flags=0x8, repeatCount=0, eventTime=22123234, downTime=22123139, deviceId=0, source=0x101 } 4: sent at 22123139000000, (unhandled) KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DEL, scanCode=14, metaState=0, flags=0x8, repeatCount=0, eventTime=22123139, downTime=22123139, deviceId=0, source=0x101 } 5: sent at 22123047000000, (unhandled) KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DEL, scanCode=14, metaState=0, flags=0x8, repeatCount=0, eventTime=22123047, downTime=22122983, deviceId=0, source=0x101 } 12-16 16:23:28.524 80-253/system_process I/WindowManager﹕ createSurface Window{41a46ca0 com.cmd.cmduniverziteti/com.cmd.cmduniverziteti.MainActivity paused=false}: DRAW NOW PENDING 12-16 16:23:29.714 1163-1179/com.cmd.cmduniverziteti W/SingleClientConnManager﹕ Invalid use of SingleClientConnManager: connection still allocated. Make sure to release the connection before allocating another one. 12-16 16:23:30.093 1163-1179/com.cmd.cmduniverziteti I/System.out﹕ Exception : Not Found 12-16 16:23:30.113 1163-1179/com.cmd.cmduniverziteti I/System.out﹕ nje error ka ndodhur ne konnektim 12-16 16:23:30.668 80-92/system_process W/InputManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@417ce870
答案 0 :(得分:0)
警告:这不会直接回答您的问题,但它确实提供了另一种实现方式。
我建议使用WebAPI而不是WCF来与Android客户端进行通信。 WebAPI实现了可以更容易被Android使用的RESTful,JSON Web服务,并且比WCF更容易编写。请参阅http://www.asp.net/web-api开始使用。如果您熟悉ASP.Net MVC,切换到WebAPI应该是轻而易举的。