如何解析laravel到json,并从android接收它

时间:2016-04-21 14:49:24

标签: php android json laravel

您好我想知道如何将我的Laravel基本Web服务连接到My Android Application?目前我尝试使用native和laravel代码。我设法从本机PHP获取json。但是当我在Laravel中尝试它时,它返回相同的json但无法在Android中接收它,错误说明了Doctype

对于我的控制器中的Laravel

    public function jsonview()
{
    $data2  =DB::table('item_list')->orderBy('id','desc')->get();
    $response = array();
    $response["status"] =1;
    $response["message"] = "getting data";
    $response["data"]=array();
    //$response["data"] = $data2;
    //or with
    foreach ($data2 as $data2) {
        $datas = array();
        //echo "lol";
        $datas["nama_item"] = $data2->nama_item;
        $datas["description"] = $data2->description;
        $datas["price"] = $data2->harga;
        $data = DB::table('users')->where("id","=",$data2->seller)->first();
        $datas["seller"] = $data->firstname;
        $datas["picture"] = $data2->pic_name;
        array_push($response["data"], $datas);
    }
    //dd(Request::json()->get('data'));

    return Response()->json(['data'=>$response["data"]]);
}

在Android中返回错误

它保持接收

Value <!DOCTYPE 

这里是完整的错误代码

04-21 21:38:06.541 14436-14514/skyvity.belisini E/JSON Parser: Error Parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

My Current Android适用于原生php,但尚未在Laravel中接收

package skyvity.belisini.Controller;

import android.util.Log;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import skyvity.belisini.Config.JSONParser;

/**
 * Created by Wilztan on 4/20/2016.
 */
public class ItemController {

//Listing Home
public  static JSONArray jsonArray= null;
public static ArrayList<String> Item = new ArrayList<String>();
public static ArrayList<String> desc = new ArrayList<String>();
public static ArrayList<String> price = new ArrayList<String>();
public static ArrayList<String> seller = new ArrayList<String>();
public static ArrayList<String> picture = new ArrayList<String>();

public static void list_item(){
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    JSONParser jsonParser= new JSONParser();
    JSONObject object jsonParser.request(JSONParser.base_url+"json",params);
    try {
        if(object.getJSONArray("data")==null){
            Log.v("not Failed in here","NULL JSON");
        }
        jsonArray = object.getJSONArray("data");
        Log.v("not Failed in here",jsonArray.toString());
        for(int i=0;i<jsonArray.length();i++){
            Log.v("notFailed here","Lol");
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Item.add(jsonObject.getString("nama_item"));
            desc.add(jsonObject.getString("description"));
            price.add(jsonObject.getString("price"));
            seller.add(jsonObject.getString("seller"));
            picture.add(jsonObject.getString("picture"));
        }
    }catch (JSONException e){
        e.printStackTrace();
        Log.v("LOL" , e.toString());
    }
}}

:d

1 个答案:

答案 0 :(得分:0)

这可能是因为您的代码显示错误页面。它是HTML页面,当然它在开头包含DOCTYPE。

只需检查日志中的错误并进行修复。