2

我在 Android 中调用 asmx Web 服务,但每次运行我的应用程序时都会出现错误。

HTTP request faild.HTTP Status Code : 400

我将 - 更改为 <10.0.2.2:port/WebService1.asmx>

我也试过

但它给出了同样的错误。

服务在 .net 中完美运行,但需要 android 的解决方案。请帮助。谢谢你。

代码:

网络服务:

[WebMethod]
    public string HelloWorld()
    {
        return "Hello World - This is nikki";
    }

MainActivity.java

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

public class MainActivity extends Activity {


 private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

    private static final String METHOD_NAME = "HelloWorld";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:1553/WebService1.asmx";  





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

    call();

}


public void call()
{
     try {

         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

         request.addProperty("passonString", "Rajapandian");

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.dotNet=true;
         envelope.setOutputSoapObject(request);

         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
         androidHttpTransport.call(SOAP_ACTION, envelope);

         Object result = (Object)envelope.getResponse();

         Toast.makeText(this, result.toString(), Toast.LENGTH_LONG).show();
     } catch (Exception e) {
         Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
         }
}
4

3 回答 3

2

所有 web 服务调用都应该在后台线程中。Android 不允许在主线程上调用 web 服务。使用异步方法。

于 2013-09-06T22:22:16.733 回答
0

400意味着Bad Request。服务器正在响应。您正在发出 HTTP 请求,只是没有遵循 API 规则。

检查文档。尝试将请求转储到控制台,看看是否缺少标题、拼写错误的参数等。还转储响应正文可能有助于您的调试。

于 2013-03-31T10:24:56.197 回答
0
private void callSubmitWebService() {

    String[] params = new String[7];
    String[] values = new String[7];
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String myAndroidDeviceId = null;

    if (telephonyManager.getDeviceId() != null) {
        myAndroidDeviceId = telephonyManager.getDeviceId();
    } else {
        myAndroidDeviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    params[0] = "block_name";
    params[1] = "tab_id";
    params[2] = "department_id";
    params[3] = "username";
    params[4] = "emp_pin";
    params[5] = "image";
    params[6] = "clocked_flag";

    values[0] = "Submit";
    values[1] = myAndroidDeviceId;
    values[2] = selectedID;
    values[3] = searchname;
    values[4] = PinNo;
    values[5] = image.toString();
    values[6] = Status;




    URL = "Your URL";
    Webservice wb = new Webservice(LandingActivity.this, URL,
            params, values);


    Log.e("url callweb()", "" + URL);

    wb.execute();


}

WbService 类如下

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;

import android.os.AsyncTask;

import android.util.Log;

public class Webservice extends AsyncTask<String, Void, String> {


    private static String URL;
    private static String params[];
    private static String values[];
    String response = null;
    String id;

    private LandingActivity landingActivity;


    // switch church
    public Webservice(LandingActivity landingActivity,
                      String URL, String params[], String values[]) {
        // TODO Auto-generated constructor stub
        this.landingActivity = landingActivity;

        this.URL = URL;
        this.params = params;
        this.values = values;


    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... arg0) {

        response = CallService();

        return response;

    }

    public String CallService() {

        String json = null;
        int responseCode = 0;
        HttpResponse response = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            int s = params.length;
            Log.e("Params length", "" + s);
            for (int i = 1; i < s; i++) {


                nameValuePairs
                        .add(new BasicNameValuePair(params[i], values[i]));

            }

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            do {

                response = client.execute(httppost);
                responseCode = response.getStatusLine().getStatusCode();
                // If you want to see the response code, you can Log it
                // out here by calling:
                // Log.d("256 Design", "statusCode: " + responseCode);
            } while (responseCode == 408);
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            json = rd.readLine();
            Log.e("json", "" + json);
        } catch (Exception e) {
            responseCode = 408;
            e.printStackTrace();
        }
        return json;

    }

protected void onPostExecute(String response) {



    if (values[0].equalsIgnoreCase("Submit"))

    {

        landingActivity.OnGetResponse_for_Submit(response);

    }

}
}

在以下功能中对 Web 服务的响应做任何事情

  public void OnGetResponse_for_Submit(String response) {

}
于 2016-02-03T05:09:22.020 回答