Problème pour accèder à un service WEB
Salut,
J'arrive pas à appeler ma méthode d'addition via mon android app
mon code android
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
package ma.ws;
import java.io.InputStream;
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 org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener {
private static String URL_WS = "http://localhost:8080/MonServiceWEB/services/AddNumber.AddNumberHttpEndpoint/";
private String resultat;
private ProgressDialog dialog;
private Handler mHandler;
private EditText txtNombre1;
private EditText txtNombre2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.btnCalculer)).setOnClickListener(this);
txtNombre1 = (EditText) this.findViewById(R.id.txtNombre1);
txtNombre2 = (EditText) this.findViewById(R.id.txtNombre2);
dialog = new ProgressDialog(this);
dialog.setMessage("Appel au webservice en cours...");
dialog.setCancelable(true);
mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
dialog.dismiss();
((TextView) findViewById(R.id.textView1)).setText(resultat);
break;
}
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCalculer:
dialog.show();
new Thread(new Runnable() {
public void run() {
resultat = convertViaWebService(txtNombre1.getText()
.toString(), txtNombre2.getText().toString());
mHandler.sendEmptyMessage(1);
};
}).start();
break;
}
}
private String convertViaWebService(String nombre1, String nombre2) {
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(URL_WS);
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("a", nombre1));
nameValuePairs.add(new BasicNameValuePair("b", nombre2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(is, HTTP.UTF_8);
char[] buf = new char[4096];
int count;
StringBuilder sb = new StringBuilder();
Log.i("JE SUIS LAAAAAAAAAAAA", "I'M HEEEEEEEEEEEEEEEEEEEER");
while ((count = reader.read(buf, 0, buf.length)) != -1)
sb.append(buf, 0, count);
is.close();
String res = sb.toString();
return String.valueOf(Math.round(Integer.parseInt(sb.toString()
.substring(res.indexOf("/\">") + 3, res.lastIndexOf("<"))))
+ " " + nombre2);
} catch (Exception e) {
return "Erreur lors de l'appel au webservice";
}
}
} |
Classe WEB SERVICE
Code:
1 2 3 4 5 6 7 8
|
public class AddNumber {
public int add(String a, String b) {
return Integer.parseInt(a) + Integer.parseInt(b);
}
} |
WSDL
Code:
1 2 3 4
|
<wsdl:definitions targetNamespace="http://service"><wsdl:documentation>
Please Type your service description here
</wsdl:documentation><wsdl:types><xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service"><xs:element name="add"><xs:complexType><xs:sequence><xs:element minOccurs="0" name="a" nillable="true" type="xs:string"/><xs:element minOccurs="0" name="b" nillable="true" type="xs:string"/></xs:sequence></xs:complexType></xs:element><xs:element name="addResponse"><xs:complexType><xs:sequence><xs:element minOccurs="0" name="return" type="xs:int"/></xs:sequence></xs:complexType></xs:element></xs:schema></wsdl:types><wsdl:message name="addRequest"><wsdl:part name="parameters" element="ns:add"/></wsdl:message><wsdl:message name="addResponse"><wsdl:part name="parameters" element="ns:addResponse"/></wsdl:message><wsdl:portType name="AddNumberPortType"><wsdl:operation name="add"><wsdl:input message="ns:addRequest" wsaw:Action="urn:add"/><wsdl:output message="ns:addResponse" wsaw:Action="urn:addResponse"/></wsdl:operation></wsdl:portType><wsdl:binding name="AddNumberSoap11Binding" type="ns:AddNumberPortType"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="add"><soap:operation soapAction="urn:add" style="document"/><wsdl:input><soap:body use="literal"/></wsdl:input><wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="AddNumberSoap12Binding" type="ns:AddNumberPortType"><soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><wsdl:operation name="add"><soap12:operation soapAction="urn:add" style="document"/><wsdl:input><soap12:body use="literal"/></wsdl:input><wsdl:output><soap12:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:binding name="AddNumberHttpBinding" type="ns:AddNumberPortType"><http:binding verb="POST"/><wsdl:operation name="add"><http:operation location="AddNumber/add"/><wsdl:input><mime:content type="text/xml" part="add"/></wsdl:input><wsdl:output><mime:content type="text/xml" part="add"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="AddNumber"><wsdl:port name="AddNumberHttpSoap11Endpoint" binding="ns:AddNumberSoap11Binding"><soap:address location="http://localhost:8080/MonServiceWEB/services/AddNumber.AddNumberHttpSoap11Endpoint/"/></wsdl:port><wsdl:port name="AddNumberHttpSoap12Endpoint" binding="ns:AddNumberSoap12Binding"><soap12:address location="http://localhost:8080/MonServiceWEB/services/AddNumber.AddNumberHttpSoap12Endpoint/"/></wsdl:port><wsdl:port name="AddNumberHttpEndpoint" binding="ns:AddNumberHttpBinding"><http:address location="http://localhost:8080/MonServiceWEB/services/AddNumber.AddNumberHttpEndpoint/"/></wsdl:port></wsdl:service></wsdl:definitions> |