2016-04-26 4 views
0

Ich versuche, einige Daten Form Android App zu Laravel Rest API zu posten. Die Daten werden konvertiert und als jsonArray veröffentlicht.
JsonArray Beitrag von Android zu Laravel Rest Fehler

Der Code funktioniert gut in HttpRequester (Firefox-Erweiterung), sondern wirft 500 interne Server Error, wenn von Android veröffentlicht.

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')); 
}); 

Dies ist, was ich bekommen sollte (HttpRequester kehrt auch das gleiche)

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

Antwort

0

Sie können Retrofit Lib verwenden, um mit Ihrer API zu kommunizieren, es ist so viel einfacher.

Retrofit doc: http://square.github.io/retrofit/

hat einen großen Tag

+0

Dank. aber ich suche keine neue api. möchte nur das Problem mit dem vorhandenen Code kennen. –

+0

Es ist keine neue API, es ist nur eine Bibliothek, um eine API wie Ihre Laravel-Anfrage aufzurufen – yozzy