2014-05-08 70 views
0

我正在創建一個應用程序,它首先從php服務器中檢索數據並以tablelayout的形式顯示它。我還在tablelayout的每一行旁邊添加了一個按鈕,一旦用戶按下按鈕,該行的全部數據就會發送到服務器。將數據從android應用程序傳遞到php服務器的錯誤

,這裏是我的按鈕的代碼

public void onClick(View v) 
    { 
     // startActivity(f1); 

     try { 
      Addtocart(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 


    } 



    void Addtocart(){ 
     try{ 

      httpclient=new DefaultHttpClient(); 
      httppost= new HttpPost("http://192.168.43.6/test/cart.php"); // make sure the url is correct. 
      //add your data 
      nameValuePairs = new ArrayList<NameValuePair>(2); 
      // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
      nameValuePairs.add(new BasicNameValuePair("product_code",q.getproduct_code().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("shaps",q.getShapes().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("pair",q.getpair().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("carats",q.getCarats().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("color",q.getColor().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("clarity",q.getClarity().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("service",q.getService().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("polish",q.getPolish().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("symetric",q.getSymetric().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("tables",q.getTables().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("measurments",q.getMeasurements().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("flourscne",q.getFlourscne().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("description",q.getDescription().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("certificated",q.getCerticated().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("ccode",q.getCcode().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("cut",q.getCut().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("total",q.getTotal().toString().trim())); 
      nameValuePairs.add(new BasicNameValuePair("file",q.getFile().toString().trim()));// $Edittext_value = $_POST['Edittext_value']; 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      //Execute HTTP Post Request 
      response=httpclient.execute(httppost); 

      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      final String response = httpclient.execute(httppost, responseHandler); 
      System.out.println("Response : " + response); 
      runOnUiThread(new Runnable() { 
       public void run() { 
        tv.setText("Response from PHP : " + response); 
        dialog.dismiss(); 
       } 
      }); 

      if(response.equalsIgnoreCase("add to cart")){ 
       runOnUiThread(new Runnable() { 
        public void run() { 
         Toast.makeText(Availability.this, "Add to cart successful", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       // startActivity(new Intent(Availability.this, LoginScreen.class)); 
      }else{ 
       showAlert(); 
      } 

     }catch(Exception e){ 
      dialog.dismiss(); 
      System.out.println("Exception : " + e.getMessage()); 
     } 
    } 
    public void showAlert(){ 
     Availability.this.runOnUiThread(new Runnable() { 
      public void run() { 
       AlertDialog.Builder builder = new AlertDialog.Builder(Availability.this); 
       builder.setTitle("Can't add to cart."); 
       builder.setMessage("Problem in adding to cart. Please try again later") 
         .setCancelable(false) 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
      } 
     }); 
    } 

這裏是從logcat的我的錯誤代碼

05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at com.diamond.traders.Availability.Addtocart(Availability.java:718) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at com.diamond.traders.Availability.onClick(Availability.java:652) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.view.View.performClick(View.java:4438) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.os.Looper.loop(Looper.java:136) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
05-08 11:26:54.521 17918-17918/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 

,這裏是我的全部代碼 Availability

回答

0
  • 你有沒有提到錯誤的類型,但錯誤在行編號718在可用性等級上,即on dialog.dismiss();可能 是因爲NULL指針異常,所以在調用此檢查之前,如果 您的對話框處於顯示狀態。

  • 實施

    如果(dialog.isShowing()){ dialog.dismiss(); }

  • 檢查,如果你的對話框被初始化或不

    如果(對話!= NULL){// 你的代碼裏面 }

相關問題