Reputation: 942
I have MySQL database. I'm trying to get some data from android using PHP.
This is my PHP code: Here I'm trying to get User's info using his id.
<?php
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["id"])) {
$id = $_GET['id'];
// get a product from products table
$result = mysql_query("SELECT * FROM app WHERE vk_id = $id");
//echo $result;
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$users = array();
$product["id"] = $result["id"];
$product["first_name"] = $result["first_name"];
$product["last_name"] = $result["last_name"];
$product["vk_id"] = $result["vk_id"];
$product["points"] = $result["points"];
// success
$response["success"] = 1;
// user node
$response["users"] = array();
array_push($response["users"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
Testing with browser gives seccess result, it means PHP code working fine
So, here is the Android part:
public class DB_read_by_id extends AsyncTask<String, String, String> {
Context context;
private static String url_read_by_id = "http://women.egeshki.ru/blockphonedb/read_by_id.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_USERS = "users";
private static final String TAG_POINTS = "points";
JSONArray users = null;
String vk_id;
String points;
JSONParser jParser = new JSONParser();
public DB_read_by_id(String _vk_id, Context _context) {
vk_id = _vk_id;
context = _context;
url_read_by_id += "?id=" + vk_id;
Log.e("DB_read_by_id", "Constructor");
}
@Override
protected String doInBackground(String... strings) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);
Log.e("DB_read_by_id", "doInBackground");
Log.e("DB_read_by_id: ", url_read_by_id);
Log.e("DB_read_by_id json ", json.toString());
// Check your log cat for JSON response
//Log.e("Users_all: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
String message = json.getString(TAG_MESSAGE);
users = json.getJSONArray(TAG_USERS);
Log.e("DB_read_by_id", "Attempt to get from db");
if (success == 1) {
JSONObject c = users.getJSONObject(0);
points = c.getString(TAG_POINTS);
Log.e("DB_read_by_id Points", points);
} else if (message.equals("No user found")) {
Log.e("DB_read_by_id", "No user found");
} else if (message.equals("Required field(s) is missing")) {
Log.e("DB_read_by_id", "Required field(s) is missing");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
Here is the logcat:
com.example.myapplication E/DB_read_by_id﹕ Constructor
com.example.myapplication E/DB_read_by_id﹕ doInBackground
com.example.myapplication E/DB_read_by_id:﹕ http://women.egeshki.ru/blockphonedb/read_by_id.php?id=150923434
com.example.myapplication E/DB_read_by_id json﹕ {"message":"No user found","success":0}
When I test from browser the link, which I get from logcat it's againg working fine. But from android it gives me an error. The problem is the link is ok, but android gives an error
Where is my mistake?
Upvotes: 0
Views: 49
Reputation: 1248
you didn't send the id to server
List<NameValuePair> params = new ArrayList<NameValuePair>();
//add this
params.add(new BasicNameValuePair("id","the value here));//
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);
Edit :after reading your comments i think the problem is that you use your parametre in the link instead off using the right way .. just try to use this :
List<NameValuePair> params = new ArrayList<NameValuePair>();
//add this
params.add(new BasicNameValuePair("id","150923434"));//
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);
try this and report
Upvotes: 1