在Android中将数据发布到服务器

时间:2014-08-26 12:47:00

标签: android post android-asynctask broadcastreceiver

我遇到了向服务器发布数据的问题。我已经检查了mozilla的rest客户端插件,它产生了正确的响应。但是,当我尝试使用我的代码时,它显示请求错误。请帮助我。

体:

{"BusinessVerifyResult":"Positive","BusinessVisitTime":"05:22PM","PK_Id":19,"IMEI":"911239254765011","BusinessVisitDate":"08\/20\/2014","Latitude":13.0347934,"Longitude":80.2381417,"UserId":"svs0p005","UnitShrtDescr":"VIZA2","BusinessRemarks":"okie"}

// Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://99.12.1.101/LeadMan/");


            try {


                JSONObject jsonObject = new JSONObject();
                jsonObject.put("PK_Id", 19);
                jsonObject.put("IMEI", "911239254765011");
                jsonObject.put("Latitude", 13.0347934);
                jsonObject.put("Longitude", 80.2381417);
                jsonObject.put("UserId", "svs0p005");
                jsonObject.put("BusinessVerifyResult", "Positive");
                jsonObject.put("BusinessVisitDate", "08/20/2014");
                jsonObject.put("BusinessRemarks", "okie");
                jsonObject.put("BusinessVisitTime", "05:22PM");
                jsonObject.put("UnitShrtDescr", "VIZA2");
                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();

                httppost.setHeader("Content-type", "application/json");
                httppost.setHeader("Accept", "application/json");

                StringEntity se = new StringEntity(json);
                httppost.setEntity(se);



                // Execute HTTP Post Request
                System.out.print(json);
                HttpResponse response = httpclient.execute(httppost);

                // for JSON:
                if(response != null)
                {
                    InputStream is = response.getEntity().getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    try {
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }


            }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

错误:

    ?<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. The exception message is 'Incoming message for operation 'MobiBusinessadd' (contract 'IBusinessVerification' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>
      <p>   at System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(Message message, Boolean isRequest)
   at System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

从服务器返回的日志:

contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'

告诉你问题,它期望原始的Json,所以从你的代码中删除Http Headers:

httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Accept", "application/json");