2014-07-11 9 views
-2

//所以...我這個web服務在數據庫連接。我有兩個webmethods:一個//用於在數據庫中搜索,另一個用於插入。我不知道我是否連接到webservice //因爲我按下任何按鈕時我的應用程序退出。你能幫助我嗎,非常感謝!在我的android應用程序中出錯

package com.example.btn; 

//import android.support.v7.app.ActionBarActivity; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
//import android.app.Activity; 
//import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
//import android.widget.TextView; 
import android.widget.Toast; 



public class MainActivity extends Activity { 

    private static String SOAP_ACTION1 = "http://localhost/CheckRecords"; 

    private static String SOAP_ACTION2 = "http://localhost/RecordData"; 

    private static String NAMESPACE = "http://localhost/"; 

    private static String METHOD_NAME1 = "CheckRecords"; 

    private static String METHOD_NAME2 = "RecordData"; 

    private static String URL = "http://http://192.168.100.176/WebS/Service1.asmx?wsdl"; 

    // Button btnCheck,btnRecord; 
    // EditText barcodein,denumirein; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button btnCheck = (Button) findViewById(R.id.btnCheck); 
     Button btnRecord = (Button) findViewById(R.id.btnRecord); 
     final EditText barcodein = (EditText) findViewById(R.id.barcodein); 
     final EditText denumirein = (EditText) findViewById(R.id.denumirein); 

    btnCheck.setOnClickListener(new View.OnClickListener() 
    { 
       @Override 
       public void onClick(View v) 
       { 
        //Initialize soap request + add parameters 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);  

       //Use this to add parameters 
       request.addProperty("newbarcode",barcodein.getText().toString()); 

       //Declare the version of the SOAP request 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       envelope.setOutputSoapObject(request); 
       envelope.dotNet = true; 

       try { 
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

        //this is the actual part that will call the webservice 
        androidHttpTransport.call(SOAP_ACTION1,envelope); 

        // Get the SoapResult from the envelope body. 
        SoapObject result = (SoapObject)envelope.bodyIn; 

        if(result != null) 
        { 
          //Get the first property and change the label text 
          denumirein.setText(result.getProperty(0).toString()); 
        } 
        else 
        { 
          Toast.makeText(getApplicationContext(), "No data found",Toast.LENGTH_LONG).show(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       } 
     }); 

    btnRecord.setOnClickListener(new View.OnClickListener() 

    { 

       @Override 
       public void onClick(View v) 

       { 
        //Initialize soap request + add parameters 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);  

       //Use this to add parameters 
       request.addProperty("newbarcode",barcodein.getText().toString()); 

       //Declare the version of the SOAP request 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       envelope.setOutputSoapObject(request); 

       envelope.dotNet = true; 

       try { 

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

        //this is the actual part that will call the webservice 

        androidHttpTransport.call(SOAP_ACTION2, envelope); 

          Toast.makeText(getApplicationContext(), "Inserted",Toast.LENGTH_LONG).show(); 


       } catch (Exception e) { 

        e.printStackTrace(); 

       } 

       } 

     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
+0

http://stackoverflow.com/questions/23353173/unlikely-myapp-has-stopped-how-can-i-solve-this – laalto

+0

發佈logcat請 – nobalG

+0

也許你會得到'NetworkOnMainThreadException'。請參閱[this](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception)問題。 – SSS

回答

0

我有兩個webMethods的:一個在數據庫搜索,一個用於插入。 我不知道我是否連接到Web服務,因爲我的應用程序退出時,我按任何按鈕。

=>因爲您正試圖直接在主UI線程上進行網絡調用。要解決此問題,您需要實施AsyncTask

而且我會請你檢查Logcat輸出至少這正好給你一個錯誤/異常日誌。

+0

我試過了,但沒有奏效。我是一個開始。我必須在asynctask中移動我的OnClickListener? –

+0

檢查'logcat'輸出。如果沒有它,不能說或建議! –

相關問題