将json发送到服务器(android)

时间:2015-05-01 12:57:10

标签: java android json

抱歉我的英文。我无法将json发送到服务器。我有错误:

  

{“message”:“客户数据为空!”,“状态”:“错误”}

这是我的例子,我必须发送json:

  

JSON示例:

{
  "company_id": "1",
  "phones": [
    "380000505050"
  ],
  "photo": "/files/clients_photos/tmp/484629825.JPG",
  "name": "sdfsdfdsf",
  "birthdate": "10.02.2014",
  "email": "sdf@sdf.ff",
  "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", "nesalexy@mail.ru"));
            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=nesalexy@mail.ru, 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;
        }

这是JSONparser类:

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 个答案:

没有答案