WCF Web Service avec JSON Format
J'ai crées un WCF avec format de retour JSON Le probleme que je peux pas appeler mon service a partir du client android et quand je mets un autre lien au niveau du code android ça fonctionne ce qui implique que le problème est dans le code C# pouvez vous m'aider
Code de mon service
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
| namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetData.txt")]
string GetData();
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetDataUsingDataContract/")]
List<CompositeType> GetDataUsingDataContract();
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
} |
Mon Fichier de configuration
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
| <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="tbl">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WcfService1.Service1Behavior" name="WcfService1.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="tbl">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:52570/Service1" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration> |
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
| package com.example.algorismi.myapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {
public final String feelURL = "http://192.168.1.5:52570/Service1.svc/GetData.txt";
// public final String feelURL = "http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt"; // Lien fonctionne
String responseString;
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyAsyncTask mytask = new MyAsyncTask();
mytask.execute("Sending parameter","Second param");
}
private class MyAsyncTask extends AsyncTask<String, Void, String>{
public StringBuffer buffer;
@Override
protected String doInBackground(String... params) {
String myFirstParam = params[0];
// StringBuffer buffer;
BufferedReader reader = null;
HttpURLConnection client = null;
try {
URL url = new URL(feelURL);
client = (HttpURLConnection) url.openConnection();
client.connect();
Log.i("Connexion", "Connexion est etablie avec succée");
buffer = new StringBuffer();
InputStream is = client.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
String line="";
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
Log.i("Connexion", buffer.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(client != null)
client.disconnect();
try {
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("Algorismi", "Terminée");
return myFirstParam;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
TextView txt = (TextView)findViewById(R.id.txtResult);
txt.setText(buffer.toString());
}
}
} |