如何在Android

时间:2016-05-02 02:03:46

标签: java php android android-studio

我是Android新手,我正在开发一个应用程序,其中我必须通过将所有书籍详细信息发布到pHp服务器来添加书籍,这里我使用httpPost调用发送所有书籍详细信息,但在pHp服务器中有一行,

    $this->get('security.context')->getToken()->getUser()

我无法简单地通过android中的httpPost来调用它。所以我真的需要帮助才能解决这个问题。所以请帮我如何在android ...提前调用它....

这是用于发送图书详情的httppost ..

    HttpClient httpClient = new DefaultHttpClient();
                            // replace with your url
    HttpPost httpPost = new HttpPost("www.example.com"); 


    //Post Data
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
    nameValuePair.add(new BasicNameValuePair("book", "name"));
    nameValuePair.add(new BasicNameValuePair("price", "rate"));
    nameValuePair.add(new BasicNameValuePair("author","author"));

    //Encoding POST data
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        // log exception
        e.printStackTrace();
    }

    //making POST request.
    try {
        HttpResponse response = httpClient.execute(httpPost);
        // write response to log
        Log.d("Http Post Response:", response.toString());
    } catch (ClientProtocolException e) {
        // Log exception
        e.printStackTrace();
    } catch (IOException e) {
        // Log exception
        e.printStackTrace();
    }

}

这是php post功能

    public function handlePOST($params, Request $request) {
    $book = new Book();

    if (isset($params['isbn'])) {
        $book->setIsbn($params['isbn']);
    }

    $book->setTitle($params['title']);

    if (isset($params['type'])) {
        $book->setFrequency($params['type']);
    }

    if (isset($params['period'])) {
        $book->setPeriod($params['period']);
    }

    if (isset($params['frequency'])) {
        $book->setFrequency($params['frequency']);
    }

    if (isset($params['type'])) {
        $book->setType($params['type']);
    }

    if (isset($params['author'])) {
        $book->setAuthor($params['author']);
    }

    if (isset($params['lang'])) {
        $book->setLang($params['lang']);
    }

    if (isset($params['pages'])) {
        $book->setPages($params['pages']);
    }

    if (isset($params['price'])) {
        $book->setPrice($params['price']);
    }

    if (isset($params['status'])) {
        $book->setStatus($params['status']);
    } else {
        $book->setStatus(1);
    }

    if (isset($params['bookCondition'])) {
        $book->setBookCondition($params['bookCondition']);
    }


    //$book->setAvailableFor($params['availableFor']);

    if (isset($params['description'])) {
        $book->setDescription($params['description']);
    }

    if (isset($params['genre'])) {
        $book->setGenre(json_encode($params['genre']));
    }
    // $repository = $this->getDoctrine()->getRepository('UserBundle:User');
 //    $user = $repository->findOneBy(
 //                   array('id' =>$params['userid'])
 //          );

    //how to call below code    
    $user = $this->get('security.context')->getToken()->getUser();
    $user->setRewardPoints($user->getRewardPoints() + 10);
    $book->setCreatedBy($user->getId());

    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($book);
    $em->flush();

   // $this->triggerBookChange($book->getTitle(), $params['type']);

    $response = new Response(json_encode($params));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

0 个答案:

没有答案