0

Android バックグラウンド サービスに問題があります。http プロトコルを使用してデータを送信すると、 ClientProtocolExceptionが表示されます

私のWebAPIは

  [AcceptVerbs("GET", "POST")]
  [ActionName("InsertLocationDetails")]
     public string InsertLocationDetails([FromBody]LocationDetails locDetails )
       {
        return objLocationService.InsertLocationDetails(locDetails.UserId,locDetails.DateTime.ToString(),
        locDetails.Latitude,locDetails.Longitude,locDetails.Status.ToString(),locDetails.Location.ToString());
   }

パラメータを持つ LocationDetails クラスがあります

    public class LocationDetails
       {
        public int UserId { get; set; }
        public Double Latitude { get; set; }
        public Double Longitude { get; set; }
        public string Status { get; set; }
        public string DateTime { get; set; }
        public string Location { get; set; }
    }

私の Android バックグラウンド コードでは、http プロトコルを使用しています。データを挿入するには、Web API サービスと通信する必要があります

 public String insertLocationDetails(int userid,String newtime,String status,double latitude,double longitude,String address,String city,String country) 
{       
    HostUrl="http://URL/ServiceName/InsertLocationDetails"; 

     HttpClient httpClient = new DefaultHttpClient();
     HttpPost httpPost = new HttpPost(HostUrl);

    try
    {  
      List<NameValuePair> params = new LinkedList<NameValuePair>();

      params.add(new BasicNameValuePair("UserId",String.valueOf(userid)));
      params.add(new BasicNameValuePair("DateTime", newtime));
      params.add(new BasicNameValuePair("Latitude",String.valueOf(latitude)));
      params.add(new BasicNameValuePair("Longitude",String.valueOf(longitude)));
      params.add(new BasicNameValuePair("Status",status));
      params.add(new BasicNameValuePair("Location",(address+","+city+","+country)));

      httpPost.setHeader("Content-Type", "application/json");
      httpPost.setHeader("Accept", "application/json");

      HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
      httpPost.setEntity(entity);

      ResponseHandler<String> handler = new BasicResponseHandler();
      result =httpClient.execute(httpPost,handler);


                        }
            catch (ClientProtocolException e)
            {  
                e.printStackTrace();
                Log.e(TAG, "ClientProtocolException in callWebService(). " + e.getMessage());
            }
            catch (IOException e)
            {  
                e.printStackTrace();
                Log.e(TAG, "IOException in callWebService(). " + e.getMessage());
            }

  return result;

        }}

実際、私のjqueryコードは正しく機能しており、値を挿入しています

私の Javascript コードは正しく動作しています

function insertLocationDetails()
{
    var url=serverUrl();

    var urlpath={
            UserId : userid,
            DateTime : datetime, 
            Latitude: latitude,
            Longitude : longitude, 
            Status: status,
            Location : address };

    $.ajax
    ({
        cache: false,
        async: true,
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        url: url +"InsertLocationDetails",
        data: JSON.stringify(urlpath),
        success: function (result) 
        {
            var result = eval(result);
            for (var property in result) 
            {

            }
        }
    });
}

私のAndroidバックグラウンドサービスの例外を理解するのを手伝ってください。私のサービスと通信するための正しいコードを教えてください

よろしくお願いします

4

1 に答える 1

1

リクエスト本文を application/x-www-form-urlencoded として送信していますが、コンテンツ タイプを JSON として設定しています。

使用する代わりに;

`httpPost.setHeader("Content-Type", "application/json");`

使用する;

`httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");`
于 2013-06-28T14:55:18.790 に答える