2017-11-19 14 views
0

私はアンドロイドアプリで使用しているWebサービスを開発するためにSLIMフレームワークを使用していますが、私はPostmanでそれらをテストしています。メソッドが許可されていないPUTまたは削除

私のGETメソッドとPOSTメソッドは完全に機能しますが、PUTまたはDELETEを呼び出そうとすると、メソッドが許可されていないことをブラウザに示すメッセージが表示されます。それは削除する必要があります。

私はこれを修正する方法を見つけることができません。

+2

だから、私達にあなたのコードを示しています。 – Wolen

+0

が投稿されました!あなたの時間に感謝@ウォーレン!! – paulomaranhao

答えて

0

私のクラスの要求:

String url = "http://paulomaranhao.000webhostapp.com/myslim2/temperaturas/" + String.valueOf(item); 

       Map<String, String> jsonParams = new HashMap<>(); 
       jsonParams.put("local", localName.getText().toString()); 
       jsonParams.put("continente_id", String.valueOf(continenteID)); 
       jsonParams.put("temp1", temp1.getText().toString()); 
       jsonParams.put("temp2", temp2.getText().toString()); 
       jsonParams.put("temp3", temp3.getText().toString()); 
       jsonParams.put("temp4", temp4.getText().toString()); 
       jsonParams.put("temp5", temp5.getText().toString()); 
       jsonParams.put("temp6", temp6.getText().toString()); 
       jsonParams.put("temp7", temp7.getText().toString()); 
       jsonParams.put("temp8", temp8.getText().toString()); 
       jsonParams.put("temp9", temp9.getText().toString()); 
       jsonParams.put("temp10", temp10.getText().toString()); 
       jsonParams.put("temp11", temp11.getText().toString()); 
       jsonParams.put("temp12", temp12.getText().toString()); 

       JsonObjectRequest putRequest = new JsonObjectRequest 
         (Request.Method.PUT, url, 
           new JSONObject(jsonParams), 
           new Response.Listener<JSONObject>() { 

            @Override 
            public void onResponse(JSONObject response) { 
             try{ 
              if(response.getBoolean("status")){ 
               Toast.makeText(NewLocal.this, response.getString("MSG"), Toast.LENGTH_SHORT).show(); 
              }else{ 
               Toast.makeText(NewLocal.this, response.getString("MSG"), Toast.LENGTH_SHORT).show(); 
              } 
             }catch(JSONException e){} 
            } 
           }, new Response.ErrorListener() { 
          @Override 
          public void onErrorResponse(VolleyError error) { 
           Toast.makeText(NewLocal.this, error.getMessage(), Toast.LENGTH_SHORT). show(); 
           return; 
          } 
         }){ 

        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 
         HashMap<String, String> headers = new HashMap<>(); 
         headers.put("Content-type", "application/json; charset=utf-8"); 
         headers.put("User-agent", System.getProperty("http.agent")); 
         return headers; 
        } 
       }; 

       // Access the RequestQueue through your singleton class. 
       MySingleton.getInstance(this).addToRequestQueue(putRequest); 

私のWebサービス:

$app->put('/api/temperaturas/local_id/{local_id}', function($request){ 
require_once('db/dbconnect.php'); 
$id = $request->getAttribute('local_id'); 
$local = $request->getParsedBody()['local']; 
$continente_id = $request->getParsedBody()['continente_id']; 
$temp1 = $request->getParsedBody()['temp1']; 
$temp2 = $request->getParsedBody()['temp2']; 
$temp3 = $request->getParsedBody()['temp3']; 
$temp4 = $request->getParsedBody()['temp4']; 
$temp5 = $request->getParsedBody()['temp5']; 
$temp6 = $request->getParsedBody()['temp6']; 
$temp7 = $request->getParsedBody()['temp7']; 
$temp8 = $request->getParsedBody()['temp8']; 
$temp9 = $request->getParsedBody()['temp9']; 
$temp10 = $request->getParsedBody()['temp10']; 
$temp11 = $request->getParsedBody()['temp11']; 
$temp12 = $request->getParsedBody()['temp12']; 
$temperaturas = array($temp1, $temp2, $temp3, $temp4, $temp5, $temp6, $temp7, $temp8, $temp9, $temp10, $temp11, $temp12); 
try{ 
    $tabela_temperaturas = $db->local_mes(); 
    $tabela_local = $db->local(); 
    $local_data = array("nome" => $local, "continente_id" => $continente_id); 
    $firstResult = $tabela_local[$id]->update($local_data); 
    for($i = 0; $i < 12; $i++){ 
     $data = array("local_id" => $firstResult["id"], 
         "mes_id" => $i+1, 
          "temperatura" => $temperaturas[$i]); 
    } 
    $result = $tabela_temperaturas[$id]->update($data); 
}catch(Exception $e){ 
    $result = ['status' => false, 'MSG' => $e->getMessage()]; 
    echo json_encode($result, JSON_UNESCAPED_UNICODE); 
} 
if($result){ 
    $result = ['status' => true, 'MSG' => "Atualizado com sucesso!"]; 
    echo json_encode($result, JSON_UNESCAPED_UNICODE); 
}else{ 
    $result = ['status' => false, 'MSG' => "Erro na atualização!"]; 
    echo json_encode($result, JSON_UNESCAPED_UNICODE); 
} 

});

私のindex.php

<?php 
use \Psr\Http\Message\ServerRequestInterface as Request; // objetos do tipo pedido que usam o psr por causa do http 
use \Psr\Http\Message\ResponseInterface as Response;  // objetos do tipo resposta que usam o psr por causa do http 

require 'vendor/autoload.php'; // ficheiro criado pelo composer e é necessário por causa das dependencias do slim 

$settings = [ 
    'settings' => [ 
     'displayErrorDetails' => true, 
    ], 
]; 

$app = new \Slim\App($settings); // o objeto principal slim para podermos 
usar os nossos endpoints 

require_once('api/localmes.php'); 
require_once('api/local.php'); 
require_once('api/continente.php'); 

$app->run(); 
?> 
関連する問題