当前位置:编程学习 > wap >>

怎么把从服务器传递过来的JSON数据去掉双引号?

服务器是用WCF的REST方法实现的,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestService1
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class Service1
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [WebGet(UriTemplate = "GetData",ResponseFormat=WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
        public String GetData()
        {
            // TODO: Replace the current implementation to return a collection of SampleItem instances
            string ss = "Hello";
            return ss;
        }
    }
}



手机客户端接收传递过来的值,代码如下:

package google.mm.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MeetingManagerActivity extends Activity {
    /** Called when the activity is first created. */

private static final String url = "http://10.0.2.2:56031/Service1/GetData";
private HttpClient httpClient;
private HttpGet httpGet;
private HttpResponse httpResponse;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findView();
        setListens();
    }

private void setListens() {
// TODO Auto-generated method stub
btn.setOnClickListener(btnListener);
}

OnClickListener btnListener = new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
httpClient = new DefaultHttpClient();
httpGet = new HttpGet(url);
try{
httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK)
{
BufferedReader buffered = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
tv.setText(buffered.readLine());
}else{
tv.setText("獲取不成功");
}
}catch(ClientProtocolException e){
e.printStackTrace();
tv.setText("1");
}catch(IOException e){
e.printStackTrace();
tv.setText("2");
}
}

};

private Button btn;
private TextView tv;

private void findView() {
// TODO Auto-generated method stub
btn = (Button)findViewById(R.id.btn);
tv = (TextView)findViewById(R.id.tv);
}
}



运行的时候,接收来的值却带了双引号:



为什么有双引号?请大侠们教我,谢谢!!!!
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,