Json对象发送到服务器解析错误

时间:2016-07-26 09:49:40

标签: android json post

我有一个需要上传一些文字的应用。

我正在使用Fast Android Networking库。 (https://github.com/amitshekhariitbhu/Fast-Android-Networking

从示例中,我复制了代码以测试服务器,但每次我都得到parseError。

我尝试使用HTTPRequester for Mozilla(Addon)并且它可以工作。

json对象如下所示:

 {
 "full_name":"john",    
 "email":"john@gmail.com",    
 "phone":"123214",     
 "mobile_phone":"+38163600100",    
 "address":"LA street 2",       
 "place":"LA",    
 "action":"new_session"
 }

在依赖关系中添加了库,在androidmanifest中添加了Internet权限和访问网络状态。

在onCreate方法中,我实现了这个(根据需要):

 AndroidNetworking.initialize(getApplicationContext());
 connect();

连接功能:

public void connect()
{
    final JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("full_name", "John");
        jsonObject.put("email", "john@gmail.com");
        jsonObject.put("phone", "123456");
        jsonObject.put("mobile_phone", "54525");
        jsonObject.put("address", "LA");
        jsonObject.put("place", "LA street 2");
        jsonObject.put("action", "new_session");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    AndroidNetworking.post("http://example.com")
            .addJSONObjectBody(jsonObject) // posting json
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("data", response.toString());

                    try {

                        Log.i("data", response.toString());
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject jresponse = response.getJSONObject(i);
                            String client_id = jresponse.getString("client_id");
                            String status = jresponse.getString("status");
                            String session_id = jresponse.getString("session_id");
                            Log.i("dataJson", client_id);
                            Log.i("dataJson", status);
                            Log.i("dataJson", session_id);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //pDialog.dismiss();
                }

                @Override
                public void onError(ANError error) {
                    if (error.getErrorCode() != 0) {
                        error.getErrorCode();
                        error.getErrorBody();
                        Log.d("dataJson", "onError errorCode : " + error.getErrorCode());
                        Log.d("dataJson", "onError errorBody : " + error.getErrorBody());
                        Log.d("dataJson", "onError errorDetail : " + error.getErrorDetail());

                    } else {

                        Log.d("dataJson", "onError errorDetail : " + error.getErrorDetail());
                        Log.d("dataJson", "onError errorCode : " + error.getErrorCode());
                        Log.d("dataJson", "onError errorBody : " + error.getErrorBody());
                        Log.d("dataJson", "obj" + jsonObject);


                    }
                }
            });


}

我注销了所有内容,看看发生了什么:

  07-26 11:45:32.090 21337-21337/? D/dataJson: onError errorDetail :parseError
  07-26 11:45:32.090 21337-21337/? D/dataJson: onError errorCode : 0
  07-26 11:45:32.090 21337-21337/? D/dataJson: onError errorBody : null
  07-26 11:45:32.090 21337-21337/? D/dataJson: obj{"full_name":"John","email":"john@gmail.com","phone":"123456","mobile_phone":"54525","address":"LA","place":"LA street 2","action":"new_session"}

我选择这个json对象,将它传递给HTTPRequester,然后就可以了。我错过了什么?

响应:

     {
     "client_id": 2,
     "status": "ok",
     "session_id": "furpnvflbsh6414hkf2k8a83i7" 
     }

0 个答案:

没有答案