duanjiwu0324 2016-04-21 14:49
浏览 61

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

Hello I would like to know how to connect my Laravel base Web service to My Android Application? Currently im tried with both native and laravel code. and i manage to get the json from native php. but then when I Tried it in Laravel it returns the same json but unable to receive it in Android, the error says something about Doctype

For The Laravel in my Controller

    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"]]);
}

Returning Error in Android

it keeps Receiving

Value <!DOCTYPE 

here are the full error code

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 Which works in native php but yet not receiving in 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条回答 默认 最新

  • dsfds656545 2016-04-21 15:32
    关注

    This happens probably because your code shows error page. And it is HTML page and of course it contains DOCTYPE at the beginning.

    Just check your logs for errors and fix them.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题