Reputation: 672
I am trying to send JSON object from Android to Laravel but I get response-code 405
Please check my code below.
Activity to make request:
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();
}
}
}
Here is the route in web.php
:
Route::get('/check', [
'uses' => 'DashboardController@check',
'as' => 'check'
]);
and the code for DashboardController
class DashboardController extends Controller
{
public function check()
{
die(request());
}
}
Please tell me what I am doing here wrong. My problem is to exchange JSON Arrays/Objects between Android and Laravel. Please guide me in right direction.
Upvotes: 0
Views: 526
Reputation:
405 means you're using the wrong http verb in your request. Your route is a GET request, you are probably POSTing to it from android.
Either that or you're not hitting the route you think you are due to route matching if you have other routes defined before this one.
Upvotes: 2