我正在尝试将一些数据从Android应用程序发布到Laravel rest api。数据将被转换并发布为jsonArray。
代码在HttpRequester(Firefox扩展)中正常工作,但从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也会返回相同的内容)
{
"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
}
答案 0 :(得分:0)