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.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大