我有以下PHP脚本:
use Illuminate\Http\Response;
use Closure;
use Auth;
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return new Response(view('login'));
}
return $next($request);
}
}
如何从android设备向<?php
$parameter=$_POST['uploaded_text'];
$data = $parameter.PHP_EOL;
$fp = fopen('uploads/test.txt', 'a');
fwrite($fp, $data);
?>
提供字符串值?
我正在寻找HttpURLConnection的教程,但我只找到'HttpClient',我仍然无法使用。
有人能告诉我怎么能干这么做吗?
提前谢谢!!
答案 0 :(得分:1)
我的建议是看一下StringRequests。以下是如何将事物发送到PHP文件的示例,该文件更新数据库,或者可以执行任何操作:
<强> LoginRequest.java:强>
package com.example.your_app.database_requests;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL = "http://example.com/Login.php";
private Map<String, String> params;
public LoginRequest(String username, String password, Response.Listener<String> listener) {
super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
发送StringRequest并获取返回值:
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
// Anything else that your php file returns will go here (like the boolean success)
if (!success) {
Log.e(TAG, "Could not login.");
} else {
Log.e(TAG, "Logged in.");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, listener);
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(loginRequest);
<强>的login.php:强>
<?php
$password = $_POST["password"];
$username = $_POST["username"];
$con = mysqli_connect("website.com", "dbusername", "dbpassword", "dbtable");
$response = array();
$response["success"] = false;
/* Do something */
$response["success"] = true;
echo json_encode($response);
?>
答案 1 :(得分:1)
网上有很多关于此类内容的教程:
try {
String url = "http://yoursiteurlhere.com" ;
URL myURL = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) myURL.openConnection();
httpURLConnection.setRequestMethod("POST");
String body = params[0];
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "application/json");
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String inputLine;
stringBuffer = new StringBuffer();
while((inputLine = bufferedReader.readLine()) != null){
stringBuffer.append(inputLine);
}
bufferedReader.close();
return stringBuffer.toString();
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
请记住在后台线程中执行此操作!
请参阅此example。