为什么 ksoap2 不工作?

Why is ksoap2 not working?

我是初学者 android 开发人员并且正在使用 Web 服务。 我不知道什么是主要问题!要么我的代码有问题,要么这个 KSoaP 东西有问题!

我没有得到结果

这是activityclass

package com.stc.nationalholidays;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;

public class Holiday extends Activity {

// http://localhost:34983/WebServiceHoliday.asmx?op=GetAboutData
private final String NAMESPACE = "http://localhost/";
private final String URL = "http://localhost:34983/WebServiceHoliday.asmx?WSDL";
private final String SOAP_ACTION = "http://localhost/GetHolidayData";
private final String METHOD_NAME = "GetHolidayData";
private String HOLIDAYS, ISO_CODE;
private TextView result;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle obj) {
    super.onCreate(obj);
    setContentView(R.layout.activity_holiday);

    result = (TextView) findViewById(R.id.holidays);
    result.setText(" ");

    final Intent intent = getIntent();

    String name = intent.getStringExtra("Data");
    String ini = name.trim(); // Data with no space character
    String code = ini.substring(0, 2); // ISO Code of the country

    ISO_CODE = code;

    // Toast.makeText(getBaseContext(), code, Toast.LENGTH_LONG).show();

    if (isOnline()) {

        AsyncCallWS task = new AsyncCallWS();
        task.execute();

        Toast.makeText(getBaseContext(),
                "Processing your request!!!\nPlease wait...",
                Toast.LENGTH_SHORT).show();
        result.setText(HOLIDAYS);

    } else {
        try {
            AlertDialog alertDialog = new AlertDialog.Builder(
                    getBaseContext()).create();

            alertDialog.setTitle("Info");
            alertDialog
                    .setMessage("No internet connection.\nPlease check your internet connectivity and click OK.");
            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            finish();
                            startActivity(intent);

                        }
                    });

            alertDialog.show();
        } catch (Exception e) {
            // Log.d(Constants.TAG, "Show Dialog: " + e.getMessage());
        }
    }
}

public boolean isOnline() {
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
            .getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

    if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
        Toast.makeText(getBaseContext(), "No Internet connection!",
                Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.holiday, 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();

    switch (id) {
    case R.id.action_help:

        break;

    case R.id.action_about:

        break;

    case R.id.action_exit:
        exitByBackKey();
        break;
    }
    return super.onOptionsItemSelected(item);
}

/*
 * public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode ==
 * KeyEvent.KEYCODE_BACK) { exitByBackKey();
 * 
 * //moveTaskToBack(false);
 * 
 * return true; } return super.onKeyDown(keyCode, event); }
 */

@SuppressWarnings("unused")
protected void exitByBackKey() {

    AlertDialog alertbox = new AlertDialog.Builder(this)
            .setMessage("Are you sure want to exit?")
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {

                            Toast.makeText(
                                    getBaseContext(),
                                    "We would love to see you again!\nHave a joyful day. :)",
                                    Toast.LENGTH_LONG).show();

                            finish();
                            // close();

                        }
                    })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                }
            }).show();

}

public void getHolidayData(String iso) {
    try {
        // Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        // Property which holds input parameters
        PropertyInfo holiday = new PropertyInfo();
        // Set Name
        holiday.setName(iso);
        // Set Value
        holiday.setValue(iso);
        // Set dataType
        holiday.setType(String.class);
        // Add the property to request object
        request.addProperty(holiday);
        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.encodingStyle = SoapEnvelope.ENC;
        envelope.setAddAdornments(false);
        envelope.implicitTypes = false;
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to "HOLIDAYS" variable
        Toast.makeText(getBaseContext(), "Result: " + response.toString(),
                Toast.LENGTH_LONG).show();

        HOLIDAYS = response.toString();
        result.setText(HOLIDAYS);

    } catch (Exception e) {
        result.setText(e.toString());
        // e.printStackTrace();
    }
}

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        // Log.i(TAG, "doInBackground");
        try {
            getHolidayData(ISO_CODE);
        } catch (Exception e) {
            result.setText(e.toString());
            //e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result1) {
        // Log.i(TAG, "onPostExecute");
        // String currencyResult = res + " " +to.toString();
        result.setText(HOLIDAYS);
    }

    @Override
    protected void onPreExecute() {
        // Log.i(TAG, "onPreExecute");
        result.setText("Fetching Data...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // Log.i(TAG, "onProgressUpdate");
        result.setText("Updating Results...");
    }

}
}

我在清单文件中拥有所有必需的权限。 Web 服务一切正常。

请帮我解决这个问题,或者我已经完成了 android!

您必须确保可以从您正在测试的 device/emulator 访问您使用的网络服务的 url。这需要是主机名或 IP 地址。 localhost 将无法工作,因为它指向 phone/device 本身...