Laravel Rest Authentication when post via HttpClient Android

与世无争的帅哥 提交于 2019-12-22 12:19:54

问题


How can I filter the laravel rest app, just authenticated userid and correct password when I post via android HttpClient? I try to find the best way, but get no best sample.

This is my code:

public static int LogExceptionError(final ExceptionLog exceptionLog) {

        final HttpResponse[] response = new HttpResponse[1];
        new Thread(new Runnable() {
            public void run() {
                // Create a new HttpClient and Post Header
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(AndroidHelperUtility.getBaseUrl("exceptionlog/create", AndroidHelperUtility.IS_DEBUG_MODE));
                try {

                    Gson gson = new Gson();
                    String jsonString = gson.toJson(exceptionLog);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject = new JSONObject(jsonString);

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(jsonObject);

                    // Post the data:
                    httpPost.setHeader("json", jsonObject.toString());
                    httpPost.getParams().setParameter("jsonpost", jsonArray);

                    // Execute HTTP Post Request
                    System.out.print(jsonObject);
                    HttpResponse httpResponseponse = httpClient.execute(httpPost);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        if (response[0] != null) {
            return 1;
        } else {
            return 0;
        }
    }

Laravel Controller:

public function postCreate() {
        $json = $_SERVER['HTTP_JSON'];
        $data = json_decode($json);  
        $object = ExceptionLogService::create($data);
        return Response::json($object);

    }

Service Class:

class ExceptionLogService extends BaseService {

    public static function create($data) {
        $object = array(
            "refNumber" => $data->refNumber,
            "source" => $data->source,
            "userName" => $data->userName,
            "description" => $data->description
        );
        ExceptionLogRepository::create($object);
        return $object;
    }

来源:https://stackoverflow.com/questions/36974163/laravel-rest-authentication-when-post-via-httpclient-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!