Java 在android中调用web服务

Java 在android中调用web服务,java,android,.net,Java,Android,.net,我在安卓系统中调用ASMXWeb服务,但每次运行应用程序时都会出现错误 HTTP request faild.HTTP Status Code : 400 我把电话改成了 我也试过了 但它给出了同样的错误 这项服务在.net中运行得非常完美,但需要针对android的解决方案。 请帮忙。 多谢各位 代码: 网络服务: [WebMethod] public string HelloWorld() { return "Hello World - This is ni

我在安卓系统中调用ASMXWeb服务,但每次运行应用程序时都会出现错误

HTTP request faild.HTTP Status Code : 400
我把电话改成了

我也试过了

但它给出了同样的错误

这项服务在.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();
         }
}

400
表示
请求错误
。服务器正在响应。您正在生成HTTP请求,只是没有遵循API规则


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

所有webservice调用都应位于后台线程中。 Android不允许在主线程上调用webservice。使用异步方法

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

    }

}
}

到我还尝试了,您是否在清单中声明了INTERNET权限?是,我已声明了INTERNET权限和网络状态权限。
  public void OnGetResponse_for_Submit(String response) {

}