Здравствуйте.
Имеется следующий простой код для тестирования SOAP-запроса к веб-сервису 1С.
UPD: Ошибочка вышла. Кидает исключение, где getMessage возвращает null. Методом научного тыка было установлено, что исключение - ClientProtocolException.
В чем может быть причина?
Код:
package pro.oksi.android.myapplication;
import android.app.Activity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Text;
import java.net.URI;
interface callback {
void callback(String response);
}
public class MainActivity extends AppCompatActivity implements callback {
private Activity act;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
act = this;
Button ok = (Button) findViewById(R.id.button);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText urlView = (EditText) findViewById(R.id.url);
EditText nameView = (EditText) findViewById(R.id.name);
EditText namespaceView = (EditText) findViewById(R.id.namespace);
EditText usernameView = (EditText) findViewById(R.id.username);
EditText passwordView = (EditText) findViewById(R.id.password);
if(urlView.getText().toString() == null) {
Log.i("Null", "null");
}
SOAP task = new SOAP(act, namespaceView.getText().toString(), urlView.getText().toString(), nameView.getText().toString(), usernameView.getText().toString(), passwordView.getText().toString());
task.execute();
}
});
}
@Override
public void callback(String arg) {
TextView response = (TextView) findViewById(R.id.textView);
response.setText(arg);
}
}
class SOAP extends AsyncTask<Void, Void, Void> {
private String username;
private String password;
private String namespace;
private String url;
private String action;
private String webservice;
private String method;
private Activity context;
private callback inter;
private String response = "";
public SOAP(Activity context, String namespace, String url, String webservice, String username, String password) {
this.context = context;
this.inter = (callback) context;
this.username = username;
this.password = password;
this.url = url;
this.namespace = namespace;
this.webservice = webservice;
this.method = "ServiceInfo";
this.action = this.namespace + "#" + this.webservice + ":" + this.method;
}
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(Void... args) {
try {
String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:utr=\"http://www.oksi.pro/?utr_siteonline\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <utr:ServiceInfo/>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
StringEntity se = new StringEntity(request, HTTP.UTF_8);
se.setContentType("text/xml");
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(new URI(this.url));
httpPost.addHeader("Content-type", "text/xml;charset=UTF-8");
httpPost.addHeader("Accept-Encoding", "gzip,deflate");
httpPost.addHeader("SOAPAction", this.action);
httpPost.addHeader("Host", "84.42.19.27:60606");
httpPost.addHeader("Content-Length", request.length() + "");
httpPost.addHeader("Connection", "Keep-Alive");
httpPost.addHeader("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");
String auth = this.username + ":" + this.password;
byte[] authRaw = auth.getBytes();
String authEncoded = Base64.encodeToString(authRaw, 0, authRaw.length, Base64.DEFAULT);
httpPost.addHeader("Authorization", "Basic " + authEncoded);
httpPost.setEntity(se);
HttpResponse response = client.execute(httpPost);
int respCode = response.getStatusLine().getStatusCode();
if(respCode >= 200 && respCode < 300) {
this.response = EntityUtils.toString(response.getEntity());
Log.i("OK", this.response);
} else {
this.response = "Code" + respCode;
}
} catch (Exception Ex) {
this.response = Ex.getMessage();
Log.i("Error query", Ex.getMessage());
}
return null;
}
protected void onPostExecute(Void arg) {
inter.callback(this.response);
}
}