Ceci est mon activité Java. Les données sont en cours d'extraction depuis le serveur. Basé sur le System.out.println("hi" +co.transaction_id)
, le même article est imprimé 5 fois. Est-ce mon problème java ou est-ce un problème php. J'ai essayé d'éditer ma taille de xml de listview pour assortir le parent ou le parent de remplissage mais cela n'aide pas.Mêmes données de listview imprimées plusieurs fois
public class connect4 extends AsyncTask<String, Void, String> {
// public static final String product_id = "product_id";
public static final String RECEIPT_INDEX = "RECEIPT_INDEX";
View view;
Activity activity;
ProductAdapter1 Adapter;
public List<ContactObject> receipt = new ArrayList<>();
Bitmap bitmap;
public connect4(Activity activity, View v, ProductAdapter1 A) {
this.activity = activity;
view = v;
Adapter = A;
}
String convertStreamToString(InputStream is) {
try {
return new java.util.Scanner(is).useDelimiter("\\A").next();
} catch (java.util.NoSuchElementException e) {
return "";
}
}
protected String doInBackground(String... arg0) {
String ipAddress = "http://10.207.140.22/apexStore2/";
try {
URL url = new URL(ipAddress +"receipt.php");
String urlParameters =
URLEncoder.encode("user_id", "UTF-8") + "=" +
URLEncoder.encode(arg0[0], "UTF-8") + "&" +
URLEncoder.encode("shipping_name", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8") + "&" +
URLEncoder.encode("shipping_address", "UTF-8") + "="
+ URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("shipping_contact", "UTF-8") + "="
+ URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("shipping_email", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("order_date", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("trx_id", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("tracking_num", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("quantity", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("product_id", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("product_title", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("product_price", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8")+ "&" +
URLEncoder.encode("product_img1", "UTF-8") + "=" +
URLEncoder.encode("???", "UTF-8");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream());
wr.writeBytes (urlParameters);
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
//System.out.println(response.toString());
JSONObject mainObject = new JSONObject(response.toString());
JSONArray uniObject = mainObject.getJSONArray("result");
for(int i = 0; i < uniObject.length(); i++) {
ContactObject co = new ContactObject();
JSONObject rowObject = uniObject.getJSONObject(i);
//EventObject co = new EventObject();
//co.user_id = rowObject.getString("user_id");
co.name = rowObject.getString("shipping_name");
co.address = rowObject.getString("shipping_address");
co.contact = rowObject.getString("shipping_contact");
co.email = rowObject.getString("shipping_email");
co.orderdate = rowObject.getString("order_date");
co.transaction_id = rowObject.getString("trx_id");
co.tracking_id = rowObject.getString("tracking_num");
co.quantity = rowObject.getString("quantity");
co.product_id = rowObject.getString("product_id");
co.product_title = rowObject.getString("product_title");
co.product_price = rowObject.getString("product_price");
co.img1 = ipAddress +"img/products/" +
rowObject.getString("product_img1");
//mContentItems.add(co);
System.out.println("hi" +co.transaction_id);
receipt.add(new ContactObject(co.name, co.address, co.contact,
co.email, co.orderdate, co.transaction_id, co.tracking_id, co.quantity,
co.product_id, co.product_title, co.product_price, co.img1));
// userLogin(co.product_id);
}
//To further break down JSON
//JSONObject oneObject = mainObject.getJSONObject("1");
//String id = oneObject.getJSONObject("id");
try{
}
finally{
connection.disconnect();
}
} catch (Exception e){
System.out.println(e.toString());
}
return "";
}
protected void onPreExecute(){
}
@Override
protected void onPostExecute(String result){
Adapter.receipt = receipt;
Adapter.notifyDataSetChanged();
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
ImageView img;
public LoadImage(ImageView img){
this.img = img;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new
URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
}else{
}
}
}
}
Ceci est mon script PHP.
<?php
include ('classes/functions.php');
if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select si.shipping_name,
si.shipping_address,
si.shipping_contact,
si.shipping_email,
o.order_date,
o.trx_id,
o.tracking_num,
o.quantity,
o.product_id,
p.product_title,
p.product_price,
p.product_img1
from shipping_infos si
inner join orders o
on si.user_id = o.user_id inner join products p on p.product_id =
o.product_id
where si.user_id='".$user_id."';";
$run_receipt_checking = mysqli_query($con, $check_receipt);
$result = array();
while($row = mysqli_fetch_array($run_receipt_checking)){
array_push($result,
array(
'shipping_name'=>$row[0],
'shipping_address'=>$row[1],
'shipping_contact'=>$row[2],
'shipping_email'=>$row[3],
'order_date'=>$row[4],
'trx_id'=>$row[5],
'tracking_num'=>$row[6],
'quantity'=>$row[7],
'product_id'=>$row[8],
'product_title'=>$row[9],
'product_price'=>$row[10],
'product_img1'=>$row[11]
));
}
echo json_encode(array("result"=>$result));
}
?>
Avez-vous essayé d'exécuter la requête check_receipt sql sur votre base de données et de voir les résultats? Est-ce qu'il donne les mêmes résultats ou des résultats uniques? –
Ne voyez aucune possibilité de duplication dans le code de l'adaptateur :) vous essayez d'exécuter la requête sur db et vérifiez si votre db a des doublons ou non :) S'il a modifié votre schéma db ajouter une contrainte unique ou une contrainte de clé primaire –
il semble que c'est un problème php. Quand j'exécute une requête SQL check_receipt, il imprime plusieurs fois les mêmes données. Comment devrais-je le changer pour le faire imprimer une seule fois? –