2011-11-07 111 views
6

我正在開發一個Android代碼,點擊一個按鈕後,通過Web服務在遠程數據庫中插入數據。我的網絡服務代碼非常簡單。它只需要一個參數,它是android的Spinner值,並將該值插入到數據庫中通過Web服務將數據從Android應用程序導入數據庫

我已經使用HttpClient進行發佈,但我無法成功完成我的任務。任何人都可以幫我解決這個問題嗎?

這裏是Web服務代碼:

  using System; 
      using System.Collections; 
      using System.ComponentModel; 
      using System.Data; 
      using System.Linq; 
      using System.Web; 
      using System.Web.Services; 
      using System.Web.Services.Protocols; 
      using System.Xml.Linq; 
      using System.Data.SqlClient; 


     namespace putdata 
      { 
     /// <summary> 
     /// Summary description for Service1 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public String put(String value) 
    { 
     int count=1; 
     int rows; 

     SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"); 
     try 
     { 
      myConnection.Open(); 
      count++; 
      SqlCommand myCommand = new SqlCommand(); 
      myCommand.Connection = myConnection; 
      myCommand.CommandText = "insert into record values('"+ count +"','" + value + "')"; 
      myCommand.Parameters.Add("@value", SqlDbType.VarChar).Value = value; 
      rows = myCommand.ExecuteNonQuery(); 
      SqlDataReader myReader = myCommand.ExecuteReader(); 

      } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      myConnection.Close(); 
     } 

     return "success"; 
    } 

     } 
    } 

這是我爲Android代碼的主要邏輯:

  public void onClick(View v) { 

     String spinnerval1=""; 
     String spinnerval2=""; 

     // Perform action on clicks 
     Toast.makeText(Mark.this, "calling web service", Toast.LENGTH_LONG).show(); 
     if (v == findViewById(R.id.button)) { 
      // prepare the dialog box 
      Button progress = (Button) findViewById(R.id.button); 
      progress.setOnClickListener((OnClickListener) Mark.this); 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx"); 


      try 
      { 
      String result; 
      JSONObject obj = new JSONObject(); 
      obj.put("Maths",spinnerval1); 
      obj.put("Science",spinnerval2); 


      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("value1", spinnerval1)); 
      nameValuePairs.add(new BasicNameValuePair("value2", spinnerval2)); 
      httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse httpResponse = httpclient.execute(httppost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 

      if (httpEntity != null) 
      { 
       InputStream is = httpEntity.getContent(); 
       result = is.toString(); 
       Log.i("Result is ", "Result: " + result); 
      } 

      } 

      catch (ClientProtocolException e1) 
      { 
       // TODO Auto-generated catch block 
      } 
      catch (IOException e) { 
       // TODO Auto-generated catch block 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 



      ProgressDialog dialog=new ProgressDialog(Mark.this); 

      // make the progress bar cancelable 
      dialog.setCancelable(true); 

      // set a message text 
      dialog.setMessage("Loading..."); 

      dialog.show(); 

     } 

    }  

這裏是logcat的:

11-07 12:20:28.970: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example/.Mark } 

    11-07 12:20:29.361: DEBUG/AndroidRuntime(299): Shutting down VM 
    11-07 12:20:29.390: DEBUG/dalvikvm(299): Debugger has detached; object registry had 1 entries 
    11-07 12:20:29.460: INFO/AndroidRuntime(299): NOTE: attach of thread 'Binder Thread #3' failed 
    11-07 12:20:29.470: INFO/ActivityManager(58): Start proc com.example for activity com.example/.Mark: pid=306 uid=10060 gids={3003, 1015} 
    11-07 12:20:31.170: INFO/ActivityManager(58): Displayed activity com.example/.Mark: 1809 ms (total 1809 ms) 
    11-07 12:20:39.341: DEBUG/dalvikvm(127): GC_EXPLICIT freed 1321 objects/73848 bytes in 161ms 
    11-07 12:20:40.981: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: [email protected] 
    11-07 12:20:44.440: DEBUG/dalvikvm(201): GC_EXPLICIT freed 109 objects/9024 bytes in 190ms 
    11-07 12:20:48.840: INFO/Result is(306): Result: [email protected] 
    11-07 12:20:50.490: DEBUG/dalvikvm(260): GC_EXPLICIT freed 759 objects/54952 bytes in 1185ms 
+0

Anroid的邏輯看起來是正確的(最多不使用JSON對象),我沒有看到你的logcat片段的任何問題。但是服務器上發生了什麼?你可以發佈服務器日誌嗎? –

+0

你是指Web服務代碼? –

+0

我男人的webservice輸出。它是客戶端 - 服務器應用程序的本質,它們在雙方(有時在中間)都有問題來源 –

回答

2

ü[R最有可能缺少這些行:(httpclient.execute之前添加的話)

httppost.setHeader("Accept", "application/json"); 
    httppost.setHeader("Content-type", "application/json"); OR 

    httppost.setHeader("Content-type", "application/x-www-form-urlencoded"); 
1

你可能有設置超時時間

你沒有爲請求

定義的內容類型,我會用一個ResponseHandler所更容易編程以執行命令

+0

是的,會添加內容類型和超時代碼。基本上,我希望在插入數據時,我應該得到一些迴應,根據我的Web服務代碼,這必須是「成功」,並且我想在獲得「成功」後再進行下一個活動。那麼我該怎麼做,並且如何使用responseHandler呢? –

1

爲幫助查出問題的原因,使網絡服務回報「成功」而不做還要別的嗎。這樣你可以驗證Web服務的httpclient通信是否正常工作。然後添加數據庫邏輯並繼續排除故障。

1

看起來問題很可能與您的Web服務或數據庫的格式,因爲你的android代碼看起來是正確的。如果我是你,我會嘗試調試Web服務來查找問題。

相關問題