服务器和Android应用程序之间的通信

时间:2017-03-23 10:41:36

标签: android database laravel server response

我尝试从Android应用程序发送信息到服务器以保存在数据库中我的代码正确运行但没有数据保存在数据库中我没有得到任何响应。我不知道我的代码中的错误在哪里

   private class postData extends AsyncTask<String, Void, String> {

      // private final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
     //       "Saving data to server. Please wait...", true);

    @Override
    protected String doInBackground(String... params) {
        // perform long running operation operation

        // SharedPreferences settings = context.getSharedPreferences(PREFS_FILE, 0);
        //String server = settings.getString("server", "");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://phone.com/request_job");
        String json = "";
        String responseStr="";

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("ticket", "welcome"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            try {
                httpclient.execute(httppost);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            // Execute HTTP Post Request
            // ResponseHandler<String> responseHandler=new BasicResponseHandler();
            //String responseBody = httpclient.execute(httppost, responseHandler);

            //  if (Boolean.parseBoolean(responseBody)) {
            //  dialog.cancel();
            // }
            HttpResponse response = httpclient.execute(httppost);

            responseStr = EntityUtils.toString(response.getEntity());




        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("HTTP Failed", e.toString());
        }
        return responseStr;
    }
    protected void onPostExecute(String responseStr) {
        super.onPostExecute(responseStr);
        Toast.makeText(getActivity(),responseStr,Toast.LENGTH_LONG).show();

        if(responseStr.equals("true")){
            // Update your Button here
           Toast.makeText(getActivity(),"donefinally",Toast.LENGTH_LONG).show();
        }

    }
}

我在服务器中的代码

          public function check_user(Request $request){
    $ticket = new ticket;// this line responsible to set data in database
    $ticket->ticket = $request->ticket;

        return response()->json(['data','true']);
}


}

1 个答案:

答案 0 :(得分:0)

在您的服务器中,执行

$ticket = new ticket;// this line responsible to set data in database
$ticket->ticket = $request->ticket;
$ticket->save(); //<-- this line will save

或者一气呵成

$ticket = Ticket::create([
    'ticket' => $request->ticket
]);
...

现在,请务必正确调用ticket课程。不确定它是大写T(票)还是小写(票)。

修改

由于它不起作用,您需要逐步调试它以查看瓶颈所在。首先,在您的函数中,只需执行

return response()->json($request->ticket); 
//This will prove that the request makes it to the server

一旦您确定要求将其发送到服务器,请尝试手动保存

之类的内容
Ticket::create([
    'ticket' => 'random string'
]);

您可以直接从浏览器调用此函数来测试它是否有效。如果数据库中没有保存任何内容,请确保模型中有$fillable数组,并且可以正确连接到数据库。