【问题标题】:writing SOAP API for android studio为 android studio 编写 SOAP API
【发布时间】:2016-08-22 08:41:26
【问题描述】:

我正在构建一个 android 应用程序,我希望它使用 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()'

【问题讨论】:

  • 请查看此网址:w3schools.com/webservices/tempconvert.asmx 它不起作用
  • 请提供您使用的是哪个demo?
  • 所以该网站无法运行。其他都好吗??
  • 我丢失了获得演示的网站名称。
  • ok @Riley B. 我给你发了一个 Url。试试这个

标签: android android-studio soap


【解决方案1】:

您的网址错误,已更改。 试试这个:

    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";

我希望它对你有用。告诉我

【讨论】:

    【解决方案2】:

    试试这个代码:

    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"/>
    

    在你的 lib 文件夹中添加 jar 文件 ksoap2-android-assembly-2.4-jar-with-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());
    }
    

    【讨论】:

    • 您的解决方案有效,但如您所见,我正在尝试链接一个按钮,以便在单击它时可以执行 SOAP Api
    • 您能否编辑一下您添加文本视图和表格行的位置,代码令人困惑。我只有一个按钮、一个 TEXTVIEW 和一个 EDIT TEXT。我只需要知道我在我拥有的代码中实际出错的地方。我目前没有任何错误,它只是崩溃了。我不知道这次崩溃发生在哪里。如果可以,请提供帮助。您的代码运行良好。
    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多