Android求职APP:右滑卡片时如何用Volley发送Job ID至服务器
Hey there, let's troubleshoot this issue step by step—sending your user ID and Job ID to the server on a right swipe should be straightforward, so let's break down where things might be going wrong and fix them.
First, Let's Rule Out Basic Setup Issues
- Check Internet Permission: Make sure you've added the internet permission in your
AndroidManifest.xml—without this, all network requests will fail silently:<uses-permission android:name="android.permission.INTERNET"/> - Validate Server Endpoint: Test your PHP API with Postman or curl first. Send a POST request with sample
user_idandjob_idparameters to confirm the server receives and responds correctly. If this fails, the issue is on the server side, not your app. - Verify User ID from SharedPreferences: Double-check that you're retrieving the user ID correctly. Add a log or toast to confirm the value isn't
nullbefore sending the request:SharedPreferences prefs = getSharedPreferences("YourAppPrefs", MODE_PRIVATE); String userId = prefs.getString("saved_user_id", null); if (userId == null) { Toast.makeText(this, "User ID not found in preferences", Toast.LENGTH_SHORT).show(); return; }
Fix the Volley Request Implementation
Most failures here come from incorrect parameter passing or missing error handling. Here's a corrected, robust version of the swipe event + Volley request:
// Inside your MainActivity's swipe listener (adjust based on your Tinder-like library) yourCardStack.setSwipeListener(new SwipeListener() { @Override public void onSwipeRight(int position) { // Get the current job from your adapter JobModel currentJob = yourJobAdapter.getItem(position); if (currentJob == null || currentJob.getJobId() == null) { Toast.makeText(MainActivity.this, "Invalid job data", Toast.LENGTH_SHORT).show(); return; } // Get valid user ID (as checked earlier) SharedPreferences prefs = getSharedPreferences("YourAppPrefs", MODE_PRIVATE); String userId = prefs.getString("saved_user_id", null); if (userId == null) { Toast.makeText(MainActivity.this, "Please log in first", Toast.LENGTH_SHORT).show(); return; } // Build Volley POST request String apiUrl = "http://your-server-domain/submit-application.php"; StringRequest submitRequest = new StringRequest(Request.Method.POST, apiUrl, response -> { // Success handling Toast.makeText(MainActivity.this, "Application sent!", Toast.LENGTH_SHORT).show(); // Optional: Remove the swiped card from the stack yourJobAdapter.removeItem(position); }, error -> { // Detailed error logging Toast.makeText(MainActivity.this, "Failed to send request: " + error.getMessage(), Toast.LENGTH_LONG).show(); Log.e("VolleyError", "Request failed: ", error); }) { @Override protected Map<String, String> getParams() { // Pass parameters correctly Map<String, String> params = new HashMap<>(); params.put("user_id", userId); params.put("job_id", currentJob.getJobId()); return params; } }; // Add request to Volley queue RequestQueue queue = Volley.newRequestQueue(MainActivity.this); queue.add(submitRequest); } // Implement other swipe listener methods (onSwipeLeft, etc.) as needed });
Check Your Job Model & Adapter
Ensure your JobModel class correctly parses and stores the Job ID from the JSON response:
public class JobModel { private String jobId; // Other job fields (title, description, etc.) // Constructor from JSON (using Gson or manual parsing) public JobModel(JSONObject json) throws JSONException { this.jobId = json.getString("job_id"); // Match your JSON key exactly // Parse other fields } // Getter for Job ID public String getJobId() { return jobId; } }
Server-Side PHP Checks
If the app request looks correct but the server isn't receiving data:
- Make sure your PHP script uses
$_POSTto retrieve parameters (not$_GET):$userId = $_POST['user_id']; $jobId = $_POST['job_id']; - Add error logging to your PHP script to debug:
error_log("Received user_id: " . $userId . ", job_id: " . $jobId); - If your app is testing on a local emulator, use
http://10.0.2.2instead oflocalhostto reach your local server.
Bonus Debug Tips
- Enable Volley debug logging to see raw request/response data:
VolleyLog.DEBUG = true; - Check for CORS issues if your server is on a different domain—add this to your PHP script's top:
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST");
内容的提问来源于stack exchange,提问作者Irshad Madappattu
相关产品推荐
相关产品推荐

