我在Android中调用asmx Web服务,但每次运行我的应用程序时都会出错。
HTTP request faild.HTTP Status Code : 400
我将 - 改为< 10.0.2.2:port / WebService1.asmx>
我也试过
但它给出了同样的错误。
服务在.net中完美,但需要Android解决方案。 请帮助。 谢谢。
代码:
WebService的:
[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();
}
}
答案 0 :(得分:2)
所有Web服务调用都应该在后台线程中。 Android不允许在主线程上调用Web服务。使用异步方法。
答案 1 :(得分:0)
400
表示Bad Request
。服务器 响应。您正在使HTTP请求正常,您只是不遵循API规则。
查看文档。尝试将请求转储到控制台,看看是否缺少标题,拼写错误参数等。同时转储响应主体可能有助于您的调试。
答案 2 :(得分: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);
}
}
}
对以下功能中的网络服务的响应做任何事情
public void OnGetResponse_for_Submit(String response) {
}