Android到ASP.. NET错误处理请求
本文关键字:处理 请求 错误 NET ASP Android | 更新日期: 2023-09-27 17:49:38
当我从Android向ASP发送请求时。. NET方法出现错误:
W/DefaultRequestDirector: Authentication error: Unable to response to any of these challenges: {}
W/MainActivity: Error 401 for URLI/jsonResultStr:: {"Message":"There was a error processing the request.","StackTrace":" ","ExceptionType":"}
这是ASP。. NET方法原型:
public string GetBuildingData(string roadId)
这是Android的sendToAsp方法:
public void sendToAsp() {
HttpPost httpPost = new HttpPost("http://madenati.alameentech.com:8082/Coding/Services/BuildingsServices.asmx/GetBuildingData");
httpPost.setHeader("content-type", "application/json");
HttpClient httpClient = new DefaultHttpClient(getHttpParameterObj(4000,4000));
JSONObject data = new JSONObject();
try {
data.put("roadId", "1");
StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String jsonResultStr = reader.readLine();
data = new JSONObject(jsonResultStr);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " );
}
Log.i("jsonResultStr : ",jsonResultStr);
} catch(Exception e) {
Log.v("Exception","Exception sendToAsp");
}
}
这是getttpparameterobj方法:
private HttpParams getHttpParameterObj(int timeOutConnection,int timeOutSocket)
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeOutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeOutSocket);
return httpParameters;
}
和Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
有什么问题吗?
使用这个类来做request:
package com.alameen.mat;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class WebService extends AsyncTask<String, String, String>
{
public String NameSpace = "";
public String MethodName = "";
SoapObject request;
SoapSerializationEnvelope envelope;
HttpTransportSE androidHttpTransport;
String URL="";
Activity context;
public WebService(Activity c, String nameSpace, String method, String url)
{
context=c;
NameSpace=nameSpace;
MethodName=method;
request = new SoapObject(NameSpace, MethodName);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
URL=url;
androidHttpTransport = new HttpTransportSE(URL,30000);
}
public void addProperty(String name,String val)
{
request.addProperty(name,val);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Log.i("jsonArray from ASP.NET: ",result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try
{
androidHttpTransport.call(params[0], envelope);
SoapObject result = (SoapObject)envelope.bodyIn;
String res = result.getProperty(0).toString();
//Log.d("result", res);
return res;
}
catch(Exception e)
{
SoapFault fault = (SoapFault)envelope.bodyIn;
Log.d("error", fault.getMessage()+"/"+fault.getCause());
e.printStackTrace();
}
return null;
}
void log(String l)
{
Log.d("status", l);
}
}
像这样调用:
public static String URL="http://... .asmx?WSDL";
WebService ws = new WebService(This, "http://tempuri.org/", "methodName", URL);
并将此库导入到您的项目中:
https://www.dropbox.com/s/j5bi6yt8x5xpdbk/ksoap2-android-assembly-2.6.0-jar-with-dependencies.jar?dl=0