JsonArray post 从 Android 到 Laravel 休息错误

JsonArray post from Android to Laravel Rest Error

我正在尝试 post 一些数据形式 android 应用到 Laravel 休息 api。数据被转换并 posted 为 jsonArray。

该代码在 HttpRequester(Firefox 扩展)中运行良好,但在 post 从 Android 编辑时抛出 500 内部服务器错误。

Android

public static int check(String token) throws IOException
{
    int flag;
    String response=Constants.EMPTY_STRING;
    URL mUrl=null;
    HttpURLConnection mConnection=null;

    String message="";
    JSONObject myObject1 = new JSONObject();
    JSONObject myObject2 = new JSONObject();
    JSONObject myObject3 = new JSONObject();

    JSONArray myArray = new JSONArray();
    JSONObject mySentObject = new JSONObject();
    try {
        myObject1.put("id","01");
        myObject1.put("name","Ali Jibran");
        myObject1.put("date","2016-04-01");
        myArray.put(myObject1);
        myObject2.put("id","02");
        myObject2.put("name","Imran Raja");
        myObject2.put("date","2016-05-03");
        myArray.put(myObject2);
        myObject3.put("id","03");
        myObject3.put("name","M.Quddus Raja");
        myObject3.put("date","2015-06-01");
        myArray.put(myObject3);
        //mySentObject.put("data",myArray.toString());
        //message = mySentObject.toString();
        message = myArray.toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    //String mQuery = Constants.APP_TOKEN_KEY
    //        + Constants.EQUALS + token + Constants.AMPERSAND + "posted=" + message;
    String mQuery = "posted=" + message;

    Log.d(Constants.APP_TAG,"================== Checking =================" );
    Log.d(Constants.APP_TAG, "check -> Query is : " +  mQuery);

    mUrl = new URL(Constants.APP_URL + "info?" + mQuery);
    mConnection = (HttpURLConnection) mUrl.openConnection();
    mConnection.setDoInput(true);
    mConnection.setDoOutput(true);
    //mConnection.setFixedLengthStreamingMode(message.getBytes().length);
    mConnection.setConnectTimeout(5000);
    mConnection.setInstanceFollowRedirects(false);
    mConnection.setRequestMethod(Constants.POST);

    mConnection.setRequestProperty("Content-type", "application/json");
    mConnection.setRequestProperty("charset", "utf-8");
    //Write Post Data

    DataOutputStream wr = new DataOutputStream(mConnection.getOutputStream());
    wr.writeUTF(message.getBytes().toString());
    wr.flush();
    wr.close();

    flag = mConnection.getResponseCode();
    if (flag != 200) {
        Log.d(Constants.APP_TAG,"Failure " + flag + " " + mConnection.getResponseMessage());
    }
    if (flag == 200){
        response = readData(mConnection);
        try
        {
            JSONObject jsonObject = new JSONObject(response);
            if(jsonObject.has("reply"))
                Log.d(Constants.APP_TAG,"Reply received -> " + jsonObject.getString("reply"));
            if(jsonObject.has("newmercs"))
                Log.d(Constants.APP_TAG,"Data received -> " + jsonObject.getString("newmercs"));
            if(jsonObject.has("count"))
                Log.d(Constants.APP_TAG,"Count received -> " + jsonObject.getString("count"));
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    return flag;
}

Laravel 5.0

 Route::post('info',function(){

//$newmercs = json_decode(Input::get('posted'),true);
$newmercs = json_decode(Input::get('posted'));
$reply = "";
$count = 0;

foreach ($newmercs as $newmerc){
    $count++;
}

if ($count > 0){
    $reply = 'Success';
}else {
    $reply = 'Failure';
}

return response()->json(compact('newmercs','reply','count'));
});

这就是我应该得到的(HttpRequester 也returns 相同)

 {
"newmercs": 
[
{
"date": "2016-04-01",
"id": "01",
"name": "Ali Jibran"
},
{
"date": "2016-05-03",
"id": "02",
"name": "Imran Raja"
},
    {
        "date": "2015-06-01",
        "id": "03",
        "name": "M.Quddus Raja"
    }
],
"reply": "Success",
"count": ​3
}

您可以使用 Retrofit Lib 与您的 API 进行通信,这非常简单。

改造文档:http://square.github.io/retrofit/

祝你有愉快的一天