【问题标题】:Android POST to WCF Service crashes (org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject)Android POST to WCF Service 崩溃(org.json.JSONException: Value <?xml of type java.lang.String cannot be convert to JSONObject)
【发布时间】:2013-06-17 14:47:07
【问题描述】:

我正在尝试 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;
    }
}

我的 Service.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;
                }
            }
        }
    }

【问题讨论】:

    标签: c# java android wcf azure


    【解决方案1】:

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

    【讨论】:

    • 我不知道该怎么做,但我的 WCF 不是已经在 J​​SON 中了吗?
    • WCF 允许您配置许多不同类型的通信机制。弄清楚您的 WCF 服务是如何设置的一种方法是将 Web 浏览器指向服务地址并将 ?singleWsdl 附加到它,例如 (localhost:8080/LoginMobile?singleWsdl)。在顶部附近寻找元素 。在那里,您应该找到有关数据格式的信息。如果它有关于 的信息,那么它正在使用 xml。
    • 服务绑定有时会在 web.config 文件中设置。
    • 在 android 端,您还可以尝试记录响应字符串以查看您返回的确切内容。
    • 我的服务说方法不允许,你能帮我看看吗? pcsstaffallocator.cloudapp.net/AllocationService.svc/…
    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多