Created
June 8, 2018 16:35
-
-
Save haerulmuttaqin/1ae6a3667adfb0b46ee31177289f8a57 to your computer and use it in GitHub Desktop.
additional, to display an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
additional, | |
To display an image: | |
/** | |
you have to send the image url from server, you change php code (read_detail.php) following this: | |
**/ | |
//read_detail.php | |
<?php | |
if ($_SERVER['REQUEST_METHOD']=='POST') { | |
$id = $_POST['id']; | |
require_once 'connect.php'; | |
$sql = "SELECT * FROM users_table WHERE id='$id' "; | |
$response = mysqli_query($conn, $sql); | |
$result = array(); | |
$result['read'] = array(); | |
if( mysqli_num_rows($response) === 1 ) { | |
if ($row = mysqli_fetch_assoc($response)) { | |
$h['name'] = $row['name'] ; | |
$h['email'] = $row['email'] ; | |
$h['image'] = $row['photo']; //add this | |
array_push($result["read"], $h); | |
$result["success"] = "1"; | |
echo json_encode($result); | |
} | |
} | |
}else { | |
$result["success"] = "0"; | |
$result["message"] = "Error!"; | |
echo json_encode($result); | |
mysqli_close($conn); | |
} | |
?> | |
//Add build.gradle | |
implementation 'com.squareup.picasso:picasso:2.71828' | |
/** | |
url image sent from server with json format, and json object (image) must be accepted using method getUserDetail () | |
**/ | |
//Edit getUserDetail like this | |
private void getUserDetail(){ | |
final ProgressDialog progressDialog = new ProgressDialog(this); | |
progressDialog.setMessage("Loading..."); | |
progressDialog.show(); | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_READ, | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
progressDialog.dismiss(); | |
Log.i(TAG, response.toString()); | |
try { | |
JSONObject jsonObject = new JSONObject(response); | |
String success = jsonObject.getString("success"); | |
JSONArray jsonArray = jsonObject.getJSONArray("read"); | |
if (success.equals("1")){ | |
for (int i =0; i < jsonArray.length(); i++){ | |
JSONObject object = jsonArray.getJSONObject(i); | |
String strName = object.getString("name").trim(); | |
String strEmail = object.getString("email").trim(); | |
//Add this for fetch image from json | |
String strImage = object.getString("image").trim(); | |
name.setText(strName); | |
email.setText(strEmail); | |
//display image from string url | |
Picasso.get().load(strImage).into(profile_image); | |
} | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
progressDialog.dismiss(); | |
Toast.makeText(HomeActivity.this, "Error Reading Detail "+e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}, | |
new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
progressDialog.dismiss(); | |
Toast.makeText(HomeActivity.this, "Error Reading Detail "+error.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
}) | |
{ | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
Map<String, String > params = new HashMap<>(); | |
params.put("id", getId); | |
return params; | |
} | |
}; | |
RequestQueue requestQueue = Volley.newRequestQueue(this); | |
requestQueue.add(stringRequest); | |
} | |
/** | |
sorry i have forgot to add, display the picture from the server. | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this and import classes:
Picasso.get().load(strImage)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(profile_image);