我是laravel的新手,这让我感到困惑。
我正在尝试使用HttpURLConnection
从我的android应用程序对其进行身份验证,并传递url即http://192.168.1.1:8000/api/login和api所需的标头。
我的异步任务类:
private String LOGIN_URL = "http://192.168.1.1:8000/api/login";
@Override
protected JSONObject doInBackground(String... strings) {
try {
String Username = strings[0];
String Password = strings[1];
JSONInput = new JSONObject();
try {
JSONInput.put("email",Username);
JSONInput.put("password",Password);
} catch (JSONException e) {
e.printStackTrace();
}
URL myUri = new URL(LOGIN_URL);
HttpURLConnection connection = (HttpURLConnection) myUri.openConnection();
connection.setRequestProperty("email",Username);
connection.setRequestProperty("password",Password);
connection.setRequestMethod("POST");
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
connection.setDoOutput(true);
inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
String result = "";
while((line = reader.readLine()) != null)
result += line;
inputStream.close();
try {
return JSONInput = new JSONObject(result);
}catch (Exception e){
Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
}
return JSONInput;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return JSONInput;
}
laravel的路由目录中的api.php :
Route::group([
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::group([
'middleware' => 'auth:api'
], function() {
Route::get('logout', 'AuthController@logout');
Route::get('user', 'AuthController@user');
});
});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
1:
我传递的网址错误吗?因为它告诉我该文件不存在。如果您能告诉我应该在网址中传递的内容,我将不胜感激。
2:
我正在传递HttpURLConnection.setRequestProperty
的标题。这是正确的方法吗?
3:
我什至还要上这条路线吗?
laravel版本为7.10.3
答案 0 :(得分:0)
1:我传递的网址错误,并将其更改为
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
this.state = {
response: "",
filter_response: "",
category_value : "",
min_price: "",
max_price: "",
color_value: "",
size_value: "",
};
}
componentDidMount() {
this.fetchData();
}
filterClick = (value) => {
this.setState({category_value: value, min_price: value, max_price: value, color_value: value,
size_value: value}, this.fetchFilterEffectData);
};
fetchData = async () => {
try {
const response = await fetch(
`http://localhost:8000/api/filter/`
);
const JsonResponse = await response.json();
this.setState({ response: JsonResponse });
}
catch (error) {
console.log(error);
}
};
fetchFilterEffectData = async () => {
try {
const filter_response = await fetch(
`http://localhost:8000/api/p_list?product_category=${this.state.category_value}&min_price=${this.state.min_price}&max_price=${this.state.max_price}&color=${this.state.color_value}&size=${this.state.size_value}`
);
const JsonFilterResponse = await filter_response.json();
this.setState({ filter_response: JsonFilterResponse });
}
catch (error) {
console.log(error);
}
};
render() {
const { response } = this.state;
if (!response) {
return "Loading...";
}
return (
<div>
//Before Effect
<div class="select_option_list">Category <i class="right fas fa-caret-down"></i> </div>
<div class="select_option_dropdown">
{response.product_categoryData.map((response) =>(
<div class="form-check form-check-outline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1"/>
<label class="form-check-label" for="inlineCheckbox1"><a href="#" onClick={() => this.filterClick(response.id)}>{response.category_title}</a></label>
</div>
))}
</div>
</div>
//After Effect
{filter_response.map((response) =>(
<div class="col-md-12">
<div class="blog-entry animate d-md-flex">
<img src={response.image} className="App-logo"/>
<div class="text text-2 pl-md-4">
<h3 class="mb-2">{response.title}</h3>
</div>
</div>
</div>
))}
</div>
);
}
}
export default App;
2:我改用http://192.168.1.1:8000/api/auth/login
并传递查询参数而不是标题。
3:我应该使用路线,这是正确的方法。