Android POST到WCF服务崩溃(org.json)JSONException: Value

本文关键字:JSONException Value lan java json org POST WCF 服务 崩溃 Android | 更新日期: 2023-09-27 18:18:56

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

Android Code:
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

My WCF Service 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;
                }
            }
        }
    }

Android POST到WCF服务崩溃(org.json)JSONException: Value <java.lan

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