0

我正在尝试 POST 到 WCF 服务,但它崩溃了。请帮助我怀疑它与返回的 POST 参数的格式有关,但不知道它到底在哪里 这是我的 android 应用程序代码

安卓代码:

public void onLoginBtnEvent(View v)
{
    //startActivity(new Intent("com.example.staffallocatorproject.LoggedIn"));
    new getLogin().execute(NRIC.getText().toString(), Password.getText().toString());
}

private class getLogin extends AsyncTask<String, Void, String>
{
    String AccountID = "0";

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        HttpPost request = new HttpPost(SERVICE_URI + "/LoginMobile");
        request.setHeader("Accept", "application/json");            
        request.setHeader("Content-type", "application/json");
        JSONStringer getCredential;
        try {
            getCredential = new JSONStringer()
                .object()
                    .key("LoginCredentials")
                        .object()                               
                            .key("NRIC").value(params[0])
                            .key("Password").value(params[1])
                        .endObject()
                    .endObject();

        StringEntity entity = new StringEntity(getCredential.toString());

        request.setEntity(entity);

        // Send request to WCF service
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        HttpEntity responseEntity = response.getEntity();

        char[] buffer = new char[(int)responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();

        JSONObject credentials = new JSONObject(new String(buffer));
        AccountID = credentials.getString("AccountID");

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        if (AccountID != "0")
        {
            Intent i = new Intent(getApplicationContext(), LoggedIn.class);
            i.putExtra("AccountID", AccountID);
            startActivity(i);
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Invalid NRIC or Password" + ", AccountID:" + AccountID, Toast.LENGTH_LONG).show();
        }
    }
}

日志猫:

>06-17 17:31:33.265: W/System.err(19753): org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
>06-17 17:31:33.265: W/System.err(19753):   at org.json.JSON.typeMismatch(JSON.java:111)
>06-17 17:31:33.265: W/System.err(19753):   at org.json.JSONObject.<init>(JSONObject.java:158)
>06-17 17:31:33.265: W/System.err(19753):   at org.json.JSONObject.<init>(JSONObject.java:171)
>06-17 17:31:33.265: W/System.err(19753):   at com.example.staffallocatorproject.MainActivity$getLogin.doInBackground(MainActivity.java:88)
>06-17 17:31:33.265: W/System.err(19753):   at com.example.staffallocatorproject.MainActivity$getLogin.doInBackground(MainActivity.java:1)
>06-17 17:31:33.265: W/System.err(19753):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
>06-17 17:31:33.273: W/System.err(19753):   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
>06-17 17:31:33.273: W/System.err(19753):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
>06-17 17:31:33.273: W/System.err(19753):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
>06-17 17:31:33.273: W/System.err(19753):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
>06-17 17:31:33.273: W/System.err(19753):   at java.lang.Thread.run(Thread.java:856)
>06-17 17:32:37.789: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.789: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.797: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.805: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.812: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.812: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.812: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.812: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.812: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.820: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.820: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.820: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.820: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.828: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.828: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.851: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection
>06-17 17:32:37.859: W/IInputConnectionWrapper(19753): getExtractedText on inactive InputConnection
>06-17 17:32:37.867: W/IInputConnectionWrapper(19753): getTextBeforeCursor on inactive InputConnection
>06-17 17:32:37.867: W/IInputConnectionWrapper(19753): getSelectedText on inactive InputConnection
>06-17 17:32:37.867: W/IInputConnectionWrapper(19753): getTextAfterCursor on inactive InputConnection
>06-17 17:32:37.867: W/IInputConnectionWrapper(19753): beginBatchEdit on inactive InputConnection
>06-17 17:32:37.867: W/IInputConnectionWrapper(19753): endBatchEdit on inactive InputConnection

我的 WCF 服务 IService:

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "LoginMobile",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    LoginCredentials GetLogin(LoginCredentials Lc);

[DataContract]
public class LoginCredentials
{
    [DataMember(Name = "AccountID")]
    public string AccountID
    {
        get;
        set;
    }
    [DataMember(Name = "NRIC")]
    public string NRIC
    {
        get;
        set;
    }
    [DataMember(Name = "Password")]
    public string Password
    {
        get;
        set;
    }
}

我的服务.cs:

    public LoginCredentials GetLogin(LoginCredentials credentials)
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["PCSDB"].ConnectionString;
        string strCommandText = "Select AccountID from Account Where NRIC=@NRIC AND Password=@Password";

        using (SqlConnection sqlConnection = new SqlConnection(strConnectionString))
        {
            sqlConnection.Open();

            using (SqlCommand sqlCommand = new SqlCommand(strCommandText, sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@NRIC", credentials.NRIC);
                sqlCommand.Parameters.AddWithValue("@Password", credentials.Password);

                int queryResult = sqlCommand.ExecuteNonQuery();
                SqlDataReader reader = sqlCommand.ExecuteReader();

                if (reader.Read())
                {
                    credentials.AccountID = reader["AccountID"].ToString();
                    return credentials;
                }
                else
                {
                    credentials.AccountID = "0";
                    return credentials;
                }
            }
        }
    }

这是一个关于如何使用 jQuery 进行页面缩放的示例:

 window.onload = function() {
 var currFFZoom = 1;
 var currIEZoom = 100;

 $('#In').on('click',function(){
     if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox
         var step = 0.02;
         currFFZoom += step; 
         $('body').css('MozTransform','scale(' + currFFZoom + ')');
     } else {
         var step = 2;
         currIEZoom += step;
         $('body').css('zoom', ' ' + currIEZoom + '%');
     }
 });

 $('#Out').on('click',function(){
     if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox
         var step = 0.02;
         currFFZoom -= step;                 
         $('body').css('MozTransform','scale(' + currFFZoom + ')');

     } else {
         var step = 2;
         currIEZoom -= step;
         $('body').css('zoom', ' ' + currIEZoom + '%');
     }
 });};


 <input type="button" id="Out" alt="Zoom Out"/>
 <input type="button" id="In" alt="Zoom In"/>

注意:我还没有在许多浏览器上测试过这个。

4

1 回答 1

0

您的 WCF 服务正在传回 xml,您正试图将其解析为 JSON。您需要将 WCF 绑定配置为使用 JSON。

于 2013-06-17T14:49:36.817 回答