【问题标题】:Send JSON to server not working(android)将 JSON 发送到服务器不工作(android)
【发布时间】:2015-05-02 12:46:02
【问题描述】:

对不起我的英语。我有活动注册,如果用户单击“注册”,我必须将 json 发送到服务器。在我有旧项目之前,然后我像这样发送 json:

    ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>();
                postInform.add(new BasicNameValuePair("php string", "my value"));

try{
operationLink.makeHttpRequest(registrationURL, "POST", postInform);
}catch(Exception e) {}

但现在我有了新的项目,但它不起作用。我有来自服务器的错误:

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

在文档服务器中,我有示例:

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"}}}

我的程序创建 asyncTask 并发送数据:

private String registrationURL = "http://crm.pavlun.info/api/register"; //working link

//code

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

                ArrayList<NameValuePair> postInform = new ArrayList<NameValuePair>();
                postInform.add(new BasicNameValuePair("company_id", "1"));
                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());
                    registration = operationLink.makeHttpRequest(registrationURL, "POST", postInform);

                    Log.e("Link", registration.toString());
                }catch(Exception e) {
                    e.printStackTrace();
                }

                return null;
            }

如果我使用这样的在线 json 发送服务器http://gurujsonrpc.appspot.com/,在链接中我把这个:http://crm.pavlun.info/api/register 在请求 JSON 字符串中我把这个

{"company_id":"1","phones":["380034505050"],"photo":"/files/clients_photos/tmp/484629825.JPG","name":"sdfsdfdsf","birthdate":"10.02.2014","email":"sdf@sdf.ff"}

我有它的网站错误发送请求时出错。 我的代码做错了什么???

我的JSONParser operationLink = new 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));

    }
}

【问题讨论】:

    标签: java php android json


    【解决方案1】:

    我尝试了您在问题中提供的两个请求示例,它们都给了我以下回复:

    Http状态:200 {"status":"error","message":"客户数据为空!"}

    所以您的问题不是来自您的问题,您似乎缺少一些字段并且服务因此引发错误,尽管 httpStatus 不应该是 200。

    我使用 chrome 中的 Advanced Rest Client 作为工具来发出休息请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多