我是创建一个WCF Web服务,我想在Android客户端中使用它我有一个问题,我找不到它 . 如果我把这个链接的其他链接:http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt我的客户工作 .
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 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; }
}
}
}
Web配置代码
<?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>
android代码
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://10.0.2.2: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());
}
}
}