1

我正在开发一个 android 应用程序,我想从 WebService 获取一些数据。我正在使用此代码从 WebService 获取 JSON 数据。

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_ActiveFair";
        HttpPost post = new HttpPost(URL);

        post.setHeader("Content-Type", "application/json; charset=utf-8");
        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("error!!",e.toString());
    }

它工作正常,我得到这样的数据:

     {
        "d": "{\"Id\":2,\"Name\":\"Fair Name \",\"IsActive\":true,\"Date_Start\":\"\\/Date(1383343200000)\\/\",\"Date_End\":\"\\/Date(1384034400000)\\/\",\"Url_Map\":null,\"Details\":\"Fair Details \",\"Address\":\"FairAdress \",\"VisitingInfo\":\"Fair Visiting Info\",\"Contact\":null,\"Transportation\":\" Fair Transportation Info \"}"
     }

但是当我想在需要获取的 web 服务中使用另一种方法时,FairId我得到了结果:

     {
        "Message": "Invalid JSON primitive: FairId.",
        "StackTrace": "   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
        "ExceptionType": "System.ArgumentException"
     }

这是我运行该Get_EventList方法的代码:

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_EventList";
        HttpPost post = new HttpPost(URL);

        List<NameValuePair> postParameters;
        postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("FairId", "2"));
        post.setEntity(new UrlEncodedFormEntity(postParameters));

        post.setHeader("Content-Type", "application/json; charset=utf-8");
        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("hata!!",e.toString());
    }

可能是什么问题?我该如何解决?

4

1 回答 1

1

FairId我通过发送到 WebService 来解决问题JSONObject。这是我的新代码:

    TextView textv=(TextView) findViewById(R.id.textv);

    try {
        HttpClient client = new DefaultHttpClient();  
        String URL = "http://server/WebService.asmx/Get_EventList";
        HttpPost post = new HttpPost(URL);


        JSONObject json = new JSONObject();
        json.put("FairId", "2");
        StringEntity se = new StringEntity( json.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);

        HttpResponse responsePost = client.execute(post);
        HttpEntity resEntityPost = responsePost.getEntity();  
        if (resEntityPost != null) 
        {
            String response=EntityUtils.toString(resEntityPost);
            Log.e("XXX",response);
            textv.setText(response);
        }
    } catch (Exception e) {
        e.printStackTrace();
        textv.setText(e.toString());
        Log.e("hata!!",e.toString());
    }
于 2013-10-22T14:14:12.590 回答