i got a code working, which enables users to select 'active' items, as in where the mysqli query is : $result = $mysqli->query("Select * From Table where id = '99'");
So when 99
is entered in an edittext 99 is showed in some textviews, except that im trying to change the 99
to a $id
variable, so that only that number if it is entered is shown by my Server.
$result = $mysqli->query("Select * From Table where id = '$id'");
Now i want the android java code, to connect and return whatever value is entered, if it is in the database. I have searched all day, for an example, but i havent found my solution, Since Name Value Pairs were used in every example/tutorial, i tried using it to, but all i get is NullpointerExceptions...
This is my JSON Parser class, as im using AsyncTask im not quite sure it can be used together, even though AsyncTask is now required to use when building Android apps.
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
String id;
private static final String TAG_ID = "id";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
PHP COde:
<?php
$mysqli = new mysqli("host", "user", "password", "database");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id=$_REQUEST['id'];
$result = $mysqli->query("Select * From Table where id = '$id'");
while($row = mysqli_fetch_assoc($result)) {
$rows=$row;
}
echo json_encode($rows);
$result->close();
$mysqli->close();?>
I was trying to follow this Example, its just that its outdated, and i cant quite follow, whats what. Most of my code, didnt come from this example, i do want to include it. Example i was following
Thank you so much for taking some time out of your day to review my problem.
0 comments:
Post a Comment