2016-04-19 91 views
3

這是我的java活動。數據正在從服務器中檢索。基於System.out.println("hi" +co.transaction_id),同一項目正在打印5次。這是我的Java問題還是它的PHP問題。我試圖編輯我的listview xml高度來匹配父母或填寫父母,但它沒有幫助。來自listview的同樣的數據被多次打印

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{ 
     } 
    } 
} 
} 

這是我的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)); 
} 
?> 
+0

您是否嘗試在數據庫上執行check_receipt sql查詢並查看結果?它是否給出相同的結果或獨特的結果? –

+0

不要在適配器代碼中看到任何重複的可能性:)你嘗試在數據庫上執行查詢並檢查你的數據庫是否有重複的條目:)如果它修改了你的數據庫模式,添加一些唯一的約束或主鍵約束來確保數據庫中的唯一性: ) –

+0

它似乎是PHP的問題。當我執行check_receipt sql查詢時,它會多次打印出相同的數據。我應該如何改變它使其僅打印一次? –

回答

0

我不知道100%,但當我處理自定義適配器時,我面臨同樣的問題。我的問題是通過更正自定義適配器的getView()中的代碼來解決的。

願幫助。

+1

經過檢查,我意識到我的錯誤在於PHP方面,可能是sql。它已經得到相同的數據5倍 –

+0

太棒了!最後它解決了。 – Adarsh