2016-10-22 54 views
1

使用getStringExtra發送到數據庫我的應用程序從mapActivity接收區域名稱,然後包括與當前活動取得的字符串是General_Info,需要發送到在線數據庫。無法使用數據排球

在PHP代碼中沒有錯誤。

我已經測試了使用wi-fi和數據的應用程序,但不幸的是,它總是返回「失敗」。

我不知道我的代碼有什麼問題。

附上我的代碼

StringRequest request=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      if(response.contains("SUCCESS")){ 
       Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show(); 

      } 
      else{ 
       Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show(); 

      } 

     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), "system error", Toast.LENGTH_SHORT).show(); 

      error.printStackTrace(); 
     } 
    }){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String,String> param=new HashMap<String, String>(); 
      param.put("householdid",houseNo.getText().toString()); 
      param.put("region", region); 
      param.put("totalmembers", totalMembers.getText().toString()); 
      param.put("totalfemale", totalFemale.getText().toString()); 
      param.put("totalmale", totalMale.getText().toString()); 

      return param; 
     } 
    }; 
    MySingleton.getInstance(General_Info.this).addToRequestQueue(request); 
} 

PHP代碼:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
    include_once("connect.php"); 

    $householdId = $_POST['householdid']; 
    $region = $_POST['region']; 
    $totalMembers = $_POST['totalmembers']; 
    $totalFemale = $_POST['totalfemale']; 
    $totalMale = $_POST['totalmale']; 
    $result = mysqli_query($con,"INSERT INTO households (householdId,region,totalMembers,totalFemale,totalMale) 
     VALUES ('$householdId',' $region','$totalMembers','$totalFemale',' $totalMale')"); 

    if($result == true) { 
     echo "SUCCESS"; 
    } 
    else{ 
     echo "FAILURE "; 
    } 
} 
mysqli_close($con); 
+0

我想你錯過了代碼:)在所有響應 –

+0

認沽日誌收到(Log.d)並粘貼您所得到的響應。此外,在粘貼代碼時使用代碼按鈕,不要像以前那樣執行代碼,並附上圖片。 – jlively

+0

使用日誌爲每個參數ü放,也記錄響應.. – Manish

回答

0

如果我是你,我一定要改變這樣的代碼。首先,我使用medoo作爲我的數據庫框架,它非常易於使用並且完全記錄在案。 如果你使用它,你的插入會是這樣:

$householdId = $_POST['householdid']; 
$region = $_POST['region']; 
$totalMembers = $_POST['totalmembers']; 
$totalFemale = $_POST['totalfemale']; 
$totalMale = $_POST['totalmale']; 

// $database is an object which you declared using medoo. 
$result = $database->insert("households",[ 
    "householdId" => $householdId, 
    "region" => $region, 
    "totalMembers" => $totalMembers, 
    "totalFemale" => $$totalFemale, 
    "totalMale" => $totalMale 
    ]); 

而且隨着documentation說的插入:

返回:[序號]最後一個插入ID

所以你只需檢查它$result是否> = 1或不是:

if($result >= 1) { 
    echo "SUCCESS"; 
} 
else{ 
    echo "FAILURE "; 
} 

我認爲它會作爲你的服務器。而對於你的客戶會是這樣的:

首先你需要一個服務發電機:

public class ServiceGenerator { 

    public static final String API_BASE_URL = "http://your.api-base.url"; 

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 

    private static Retrofit.Builder builder = 
     new Retrofit.Builder() 
       .baseUrl(API_BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()); 

    public static <S> S createService(Class<S> serviceClass) { 
     Retrofit retrofit = builder.client(httpClient.build()).build(); 
     return retrofit.create(serviceClass); 
    } 
} 

然後,你必須編寫自己的接口......可以說,我們有您的Web服務是這樣的:http://your.api-base.url/my-service/insert.php

而且你的接口:

public interface MyClient { 
    @POST("/myservice/insert.php") 
    Call<String> insert(
     @Body("householdid") String householdId, 
     @Body("region") String region, 
     @Body("totalmembers") String totalMembers, 
     @Body("totalfemale") String totalFemale, 
     @Body("totalmale") String totalMale 
    ); 
} 

好了,現在一切準備就緒。

調用該方法,你應該這樣做:

MyClient client = ServiceGenerator.createService(MyClient.class); 
Call<String> insertCall = client.insert(houseNo.getText().toString(), 
     region, 
     totalMembers.getText().toString(), 
     totalFemale.getText().toString(), 
     totalMale.getText().toString()); 

     insertCall.enqueue(new Callback<String>() { 
       @Override 
       public void onResponse(Call<String> call, Response<String> response) { 
        if(response != null && response.isSuccessful()){ 
         // you have a response...handle it 
        } 
       } 
       @Override 
       public void onFailure(Call<OwnerResponse> call, Throwable t) { 
        //PROBLEM!!! 
       } 
      } 
+0

謝謝@Mohammad Z :)已經解決了這個問題,事實上我的數據庫結構有問題,你發佈的所有內容都幫助我。謝謝你 – Hayrooney