2016-08-22 75 views
2

我正在構建一個android應用程序,我希望它使用SOAP API進行請求和響應。 我目前正在使用我得到的一個例子,但這個例子不起作用,我已經問過主機但他沒有回覆。 所以這裏是代碼;爲android studio編寫SOAP API

Button bt; 
String TAG = "Response"; 
SoapPrimitive resultString; 
EditText celsius; 
String getCel; 

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


    bt = (Button) findViewById(R.id.bt); 
    celsius = (EditText) findViewById(R.id.cel); 

    //add button listener 
    bt.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      getCel = celsius.getText().toString(); 
      AsyncCallWS task = new AsyncCallWS(); 
      task.execute(); 

     } 
    }); 
} 

private class AsyncCallWS extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     Log.i(TAG, "onPreExecute"); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     Log.i(TAG, "doInBackground"); 
     Calculate(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     Log.i(TAG, "onPostExecute"); 
     Toast.makeText(MainActivity.this, "Response" + resultString.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 


public void Calculate() { 
    String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit"; 
    String METHOD_NAME = "CelsiusToFahrenheit"; 
    String NAMESPACE = "http://www.w3schools.com/webservices/"; 
    String URL = "http://www.w3schools.com/webservices/tempconvert.asmx"; 

    try { 
     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
     Request.addProperty("Celsius", getCel); 

     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     soapEnvelope.dotNet = true; 
     soapEnvelope.setOutputSoapObject(Request); 

     HttpTransportSE transport = new HttpTransportSE(URL); 

     transport.call(SOAP_ACTION, soapEnvelope); 
     resultString = (SoapPrimitive) soapEnvelope.getResponse(); 

     Log.i(TAG, "Result Celsius: " + resultString); 
    } catch (Exception ex) { 
     Log.e(TAG, "Error: " + ex.getMessage()); 
    } 

我收到它說一個錯誤:「)java.lang.String中org.ksoap2.serialization.SoapPrimitive.toString(」嘗試調用虛擬方法上一個空對象引用

+0

Plz chk此網址:http://www.w3schools.com/webservices/tempconvert.asmx它不起作用 –

+0

請提供您有哪些演示使用? –

+0

所以網站不工作。一切都好嗎? –

回答

0

嘗試這個代碼:

public class TabelActivity extends AppCompatActivity { 
private static String SOAP_ACTION = "http://tempuri.org/GetIDSSupportData"; 

private static String NAMESPACE = "http://tempuri.org/"; 
private static String METHOD_NAME = "GetIDSSupportData"; 
//change url here 
private static String URL = "http://166.62.119.183:8066/IDSSupportData.asmx"; 
String pksupportid,userid,username,email,subject,desc,entrydate,modatetime,issolved,imgsrc; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_table); 
    if (Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 

    bt = (Button) findViewById(R.id.bt); 
    //add button listener 
bt.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("Pageno","1"); 

    request.addProperty("PageSize","20"); 
    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_ACTION, 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 
      Log.i("result", result.toString()); 
     /* JSONObject jsonObj = new JSONObject(result.toString()); 
      Log.i("jobj",jsonObj.toString());*/ 
     /* Gson gson = new Gson(); 
      String json = gson.toJson(result.toString());*/ 
      //Log.i("json",json); 
      TableLayout tv = (TableLayout) findViewById(R.id.maintable); 

      tv.removeAllViewsInLayout(); 

      SoapObject root = (SoapObject) result.getProperty(0); 
      // Log.i("root", root.toString()); 
      SoapObject s_deals = (SoapObject) root.getProperty("NewDataSet"); 

      // Log.i("sd0", s_deals.toString()); 

      int flag = 1; 

      for(int i= -1;i<s_deals.getPropertyCount();i++){ 

       TableRow tr = new TableRow(TabelActivity.this); 

       tr.setLayoutParams(new TableRow.LayoutParams(

         TableRow.LayoutParams.FILL_PARENT, 
         TableRow.LayoutParams.WRAP_CONTENT)); 
       if (flag == 1) { 


         TextView b6 = new TextView(TabelActivity.this); 

         b6.setText("Id"); 

         b6.setTextColor(Color.BLUE); 

         b6.setTextSize(15); 

         tr.addView(b6); 

         TextView b19 = new TextView(TabelActivity.this); 

         b19.setPadding(10, 0, 0, 0); 

         b19.setTextSize(15); 

         b19.setText("Name"); 

         b19.setTextColor(Color.BLUE); 

         tr.addView(b19); 

         TextView b29 = new TextView(TabelActivity.this); 

         b29.setPadding(10, 0, 0, 0); 

         b29.setText("Status"); 

         b29.setTextColor(Color.BLUE); 

         b29.setTextSize(15); 

         tr.addView(b29); 

         tv.addView(tr); 

         final View vline = new View(TabelActivity.this); 

         vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 

         vline.setBackgroundColor(Color.BLUE); 

         tv.addView(vline); 

         flag = 0; 

        } 
       else { 

        SoapObject s_deals_1 = (SoapObject) s_deals.getProperty(i); 

        pksupportid = s_deals_1.getProperty("PkSupportId").toString(); 
        userid = s_deals_1.getProperty("UserId").toString(); 
        username = s_deals_1.getProperty("UserName").toString(); 
        email = s_deals_1.getProperty("EmailId").toString(); 
        subject = s_deals_1.getProperty("Subject").toString(); 
        desc = s_deals_1.getProperty("Description").toString(); 
        issolved = s_deals_1.getProperty("IsSolved").toString(); 
        entrydate = s_deals_1.getProperty("EntryDate").toString(); 
        imgsrc = "http://media.westsidechevrolet.com/houston/624064%20wtf.jpg"; 
        TextView b = new TextView(TabelActivity.this); 

        String stime = pksupportid; 

        b.setText(stime); 

        b.setTextColor(Color.RED); 

        b.setTextSize(15); 

        tr.addView(b); 


        TextView b1 = new TextView(TabelActivity.this); 

        b1.setPadding(10, 0, 0, 0); 

        b1.setTextSize(15); 

        String stime1 = username; 

        b1.setText(stime1); 

        b1.setTextColor(Color.BLACK); 

        tr.addView(b1); 

        TextView b2 = new TextView(TabelActivity.this); 
        b2.setPadding(10, 0, 0, 0); 

        String stime2 = desc; 

        b2.setText(stime2); 

        b2.setTextColor(Color.BLACK); 

        b2.setTextSize(15); 

        tr.addView(b2); 

        tv.addView(tr); 




        final View vline1 = new View(TabelActivity.this); 

        vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 

        vline1.setBackgroundColor(Color.WHITE); 

        tv.addView(vline1); 


       } 


      } 





     } else { 
      Toast.makeText(getApplicationContext(), "No Response", Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
    } 
    }); 
    } 
清單檔案中的

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

附加jar文件在你的lib folde [R ksoap2-機器人組裝-2.4-JAR-與-dependencies.jar在你的應用水平gradle這個文件

compile fileTree(include: '*.jar', dir: 'libs') 
compile files('libs/ksoap2-android-assembly-2.4-jar-with-dependencies.jar') 

請改變你的代碼,並檢查它:

public void Calculate() { 
String SOAP_ACTION = "http://www.w3schools.com/xml/CelsiusToFahrenheit"; 
String METHOD_NAME = "CelsiusToFahrenheit"; 
String NAMESPACE = "http://www.w3schools.com/xml/"; 
String URL = "http://www.w3schools.com/xml/tempconvert.asmx"; 

try { 
    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
    Request.addProperty("Celsius", getCel); 

    SoapSerializationEnvelope soapEnvelope = new  

    SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(Request); 

    HttpTransportSE transport = new HttpTransportSE(URL); 

    transport.call(SOAP_ACTION, soapEnvelope); 
    resultString = (SoapPrimitive) soapEnvelope.getResponse(); 

    Log.i(TAG, "Result Celsius: " + resultString); 
} catch (Exception ex) { 
    Log.e(TAG, "Error: " + ex.getMessage()); 
} 
+0

,但正如你所看到的,我試圖鏈接一個按鈕,這樣當點擊它時可以做SOAP Api –

+0

@RileyB plz檢查我編輯的答案 –

+0

@RileyB讓我知道它是否工作或不是 –

1

您的URL錯誤,它們已被更改。 試試這個:

String SOAP_ACTION = "http://www.w3schools.com/xml/CelsiusToFahrenheit"; 
    String METHOD_NAME = "CelsiusToFahrenheit"; 
    String NAMESPACE = "http://www.w3schools.com/xml/"; 
    String URL = "http://www.w3schools.com/xml/tempconvert.asmx"; 

我希望它適合你。讓我知道