Php 从Android向Laravel发送JSON
我正在尝试将JSON对象从Android发送到Laravel,但我得到了响应代码Php 从Android向Laravel发送JSON,php,android,json,laravel,http,Php,Android,Json,Laravel,Http,我正在尝试将JSON对象从Android发送到Laravel,但我得到了响应代码405请检查下面的代码 提出请求的活动: public class ConActivity extends AppCompatActivity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCont
405
请检查下面的代码
提出请求的活动:
public class ConActivity extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ConnectionTask().execute("http://192.168.100.25:8000/check");
}
public class ConnectionTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
JSONObject deviceInfo = getDeviceInfo();
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(strings[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(15000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
int responseCode=connection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new
InputStreamReader(
connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
return sb.toString();
}
else {
return new String("false : " + responseCode);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(connection != null){
connection.disconnect();
}
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
tvOut.setText(s);
}
private JSONObject getDeviceInfo(){
JSONObject json = new JSONObject();
try {
json.put("first_name", "John");
json.put("last_name", "Doe");
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
private String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
}
和DashboardController的代码
class DashboardController extends Controller
{
public function check()
{
die(request());
}
}
请告诉我我做错了什么。我的问题是在Android和Laravel之间交换JSON数组/对象。请指导我正确的方向。405表示您在请求中使用了错误的http动词。你的路线是一个GET请求,你可能是从android发布到它的
如果在此之前定义了其他路线,则可能是由于路线匹配而导致的路线未到达 405表示您在请求中使用了错误的http动词。你的路线是一个GET请求,你可能是从android发布到它的
如果在此之前定义了其他路线,则可能是由于路线匹配而导致的路线未到达 路由和请求都使用了“GET”动词,但通过将两者替换为“POST”解决了问题路由和请求都使用了“GET”动词,但通过将两者替换为“POST”解决了问题
class DashboardController extends Controller
{
public function check()
{
die(request());
}
}