2015-05-01 143 views
-4

對不起,我的英語。我不能發送json到服務器。我有錯誤:發送json到服務器(android)

{"message":"Customer data is empty!","status":"error"}

它我的例子中,HOV我必須發送JSON:

JSON example:

{ 
    "company_id": "1", 
    "phones": [ 
    "380000505050" 
    ], 
    "photo": "/files/clients_photos/tmp/484629825.JPG", 
    "name": "sdfsdfdsf", 
    "birthdate": "10.02.2014", 
    "email": "[email protected]", 
    "cars": { 
    "1": { 
     "car_brand_id": "9", 
     "car_model_id": "856", 
     "number": "AE5884AH", 
     "photo": "/files/clients_photos/tmp/484629824.JPG" 
    } 
    } 
} 

這是鏈接,在這裏我送JSON http://crm.pavlun.info/api/register

這是我的代碼:

protected Void doInBackground(String... params) { 
      JSONParser operationLink = new JSONParser(); 

      ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>(); 
      postInform.add(new BasicNameValuePair("company_id", "2")); 
      postInform.add(new BasicNameValuePair("phones", "380950466589")); 
      postInform.add(new BasicNameValuePair("name", "Alexy")); 
      postInform.add(new BasicNameValuePair("birthdate", "12.03.2014")); 
      postInform.add(new BasicNameValuePair("email", "[email protected]")); 
      postInform.add(new BasicNameValuePair("photo", "/files/clients_photos/tmp/484629825.JPG")); 

      JSONObject registration = null; 

      try { 
       Log.e("perform link", postInform.toString()); //its output [company_id=2, phones=380950466589, name=Alexy, birthdate=12.03.2014, [email protected], photo=/files/clients_photos/tmp/484629825.JPG] 

       registration = operationLink.makeHttpRequest(registrationURL, "POST", postInform); 
       Log.e("Link", registration.toString()); //its output {"message":"Customer data is empty!","status":"error"} 
      }catch(Exception e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

這是JSO Nparser類:

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET mehtod 
    public JSONObject makeHttpRequest(String url, String method, 
      List<NameValuePair> params) throws JSONException { 

     // Making HTTP request 
     try { 

      // check for request method 
      if(method == "POST"){ 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method == "GET"){ 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      }   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 
     //return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); 

    } 
} 
+0

您應該檢查您正在與之通信的服務器的文檔,以查看此錯誤的含義以及導致該錯誤的原因。 – Egor

+0

你絕對做錯了。如果你想發送一個json數據來切斷,那麼如果你的標籤以'JSONObject'開始,並且如果它'JSONArray'那麼也把數據放在那裏,你就需要把你的數據放到'JSONObject'中。 – Piyush

回答

0

我相信我有同樣的問題之前,奇怪的是,在服務器他們接受後數據的方式不同。

這裏有一個例子

以下方法適用於一個jetty服務器,但不Play

public static void sendPost(String data,String url) throws Exception{ 

    org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient(); 
    org.apache.http.client.methods.HttpPost post = new org.apache.http.client.methods.HttpPost(url); 
    org.apache.http.entity.StringEntity entity = new org.apache.http.entity.StringEntity(data.toString()); 
    post.setEntity(entity); 
    client.execute(post); 

} 

的followig方法適用於Play服務器,但不jetty

public static void sendPostV2(String data, String url) throws Exception{ 

    org.apache.commons.httpclient.HttpClient client = 
      new org.apache.commons.httpclient.HttpClient(); 
    org.apache.commons.httpclient.methods.PostMethod method = 
      new org.apache.commons.httpclient.methods.PostMethod(url); 
    method.addParameter("data", data); 
    client.executeMethod(method); 
    method.releaseConnection(); 

} 

我們仍然沒有想出爲什麼,但哦,無論什麼作品寶貝。

在你的情況下,請隨意使用以下任何方法(注意下載所需的apache包)。希望他們中的一個適合你。