2013-10-08 35 views
1

有沒有什麼辦法可以使用REST使用REST從.NET使用WCF發送JSON?目的是從android移動客戶端發送一個字符串到.NET服務器,追加一個字符串並將其發回給android客戶端。搜索只讓我產生了SOAP,這對於效率問題不推薦。未使用WCF的JSON REST

+0

可以使用的.asmx服務,但現在wcf今天更受歡迎。 WCF還支持Restful JSON請求。 – Piyush

+0

請問您可以指向一個鏈接,.asmx支持restfull json嗎?如果是,我怎麼能從我的android客戶端調用服務? – user2818487

回答

0

(安卓)客戶端:

public String SendToWebFunc(List<NameValuePair> postParameters, String URL) 
{ 
HttpClient client1 = new DefaultHttpClient(); 
HttpPost request = new HttpPost(URL); 
String page=""; 
try 
{ 
    request.setEntity(new UrlEncodedFormEntity(postParameters)); 
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
    request.setEntity(formEntity); 
    HttpResponse response; 
    response = client1.execute(request); //Sends the actual request 

    //Decode response to string 
    BufferedReader in = new BufferedReader(new  InputStreamReader(response.getEntity().getContent())); 
    String line1; 
    line1 = in.readLine(); 

    while(line1!=null) 
    { 
    page=page+line1; 
     line1=in.readLine(); 
    } 

}   
catch (ClientProtocolException e) 
{ 
      e.printStackTrace(); 
} catch (IOException e) { 
      e.printStackTrace(); 
} 
return page; 
} 

你可以有網址爲

http://IP_Address/Login/Register.ashx 

在服務器ashx的服務:

public class Register : IHttpHandler 
{ 

    public void ProcessRequest (HttpContext context) 
    { 
     String data = context.Request.Params["request_data"]; 

     . 
     . 
     . 
     context.Response.Write("false"); 
    } 
} 
+0

如果我需要將一些東西發回客戶端,該怎麼辦? – user2818487