首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未从Android应用程序运行的用户身份验证web服务代码

问未从Android应用程序运行的用户身份验证web服务代码
EN

Stack Overflow用户
提问于 2011-08-14 21:29:49
回答 1查看 595关注 0票数 0

在Android应用程序中调用web服务进行用户身份验证时,我遇到了一个问题。当我输入我知道正确的用户名和密码时,它会在模拟器屏幕上显示一个奇怪的xml错误

下面是我的Android代码:

代码语言:javascript
复制
           package com.demo;
           import android.app.Activity;
           import android.os.Bundle;
           import java.io.BufferedReader;
           import java.io.IOException;
           import java.io.InputStream;
           import java.io.InputStreamReader;
           import java.util.ArrayList;
           import java.util.List;

           import org.apache.http.HttpResponse;
           import org.apache.http.NameValuePair;
           import org.apache.http.client.ClientProtocolException;
           import org.apache.http.client.HttpClient;
           import org.apache.http.client.entity.UrlEncodedFormEntity;
           import org.apache.http.client.methods.HttpPost;
           import org.apache.http.impl.client.DefaultHttpClient;
           import org.apache.http.message.BasicNameValuePair;

          //import android.app.Activity; 
          //import android.os.Bundle;
            import android.util.Log;
            import android.view.View;
            import android.view.View.OnClickListener; 
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.TextView;

          public class AndroidLogin extends Activity implements OnClickListener {
           /** Called when the activity is first created. */
            Button ok,back,exit;
          TextView result;
            @Override
              public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         ok =(Button)findViewById(R.id.btn_login);
            ok.setOnClickListener(this);

            result = (TextView)findViewById(R.id.lbl_result);

         }

          public void postLoginData() {
           // Create a new HttpClient and Post Header
          HttpClient httpclient = new DefaultHttpClient();


        HttpPost httppost = new HttpPost("http://10.0.2.2/testlogin/Service1.asmx");

          try {
        // Add user name and password
     EditText uname = (EditText)findViewById(R.id.txt_username); 
     String username = uname.getText().toString();

     EditText pword = (EditText)findViewById(R.id.txt_password);
     String password = pword.getText().toString();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        Log.w("demo", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httppost);

        String str = inputStreamToString(response.getEntity().getContent()).toString();
        Log.w("demo", str);

        if(str.toString().equalsIgnoreCase("true"))
        {
         Log.w("demo", "TRUE");
         result.setText("Login successful");   
        }else
        {
         Log.w("demo", "FALSE");
         result.setText(str);             
        }

    } catch (ClientProtocolException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
} 

      private StringBuilder inputStreamToString(InputStream is) {
         String line = "";
         StringBuilder total = new StringBuilder();
 // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
 // Read response until the end
        try {
          while ((line = rd.readLine()) != null) { 
          total.append(line); 
  }
       } catch (IOException e) {
        e.printStackTrace();
        }
 // Return full string
        return total;
         }

@Override
public void onClick(View view) {
  if(view == ok){
    postLoginData();
  }
}
  }

这是我在模拟器上输出的图片:http://www.flickr.com/photos/66352826@N07/6041731494/这里是logcat:http://pastebin.com/YmH0h6SF

请谁来帮帮我

EN

回答 1

Stack Overflow用户

发布于 2011-08-14 21:50:11

我不是.net专家,但是从日志记录来看,服务器端似乎出了问题。这里有一个类似问题的链接(建议您将debbuger附加到服务器端,以获取更多详细信息):http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/fdb3c1f0-9e23-493b-ab6a-1c7e69c7305f/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7057068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档