0

アクティビティには、ユーザー名とパスワードを取得するための 2 つの編集テキストと、アクションのための 1 つのボタンがあります。これがリスナーコードです。

Button sendBtn = (Button) findViewById(R.id.sendBtn);
        sendBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            EditText un = (EditText) findViewById(R.id.usernameEditText);

            EditText pas = (EditText) findViewById(R.id.passwordEditText);
            HttpResponse response = null;

            user_name = un.getText().toString();
            password = pas.getText().toString();
            path = p + user_name;
            my_map = createMap();
            JSONObject ob=null;


            try {
                ob = new JSONObject("{\"Username\":user_name,\"Password\":password}");
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            try {
                response = makeRequest(path, my_map,ob);
                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
                String json = reader.readLine();
                JSONTokener tokener = new JSONTokener(json);
                JSONArray finalResult = new JSONArray(tokener);
            } catch (Exception e) {
                Log.e("HTTP ERROR", e.toString());
            }
        }
    });

makeRequest 関数は次のとおりです。

public static HttpResponse makeRequest(String path, Map params,JSONObject obj) throws Exception 
    {   


        DefaultHttpClient httpclient = null;
        HttpPost httpost = null;
        ResponseHandler responseHandler = null;
        //instantiates httpclient to make request

            httpclient = new DefaultHttpClient();

            //url with the post data
            HttpGet httpget = new HttpGet(path);
            httpost = new HttpPost(path);

            //convert parameters into JSON object
            JSONObject holder = obj;


            //passes the results to a string builder/entity
            StringEntity se = new StringEntity(holder.toString(), HTTP.UTF_8);

            //sets the post request as the resulting string
            httpost.setEntity(se);
            //sets a request header so the page receving the request
            //will know what to do with it

            httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");
        try{
            //Handles what is returned from the page 
            responseHandler = new BasicResponseHandler();
        }catch(Exception e){
            Log.e("HTTP ERROR", e.toString());
        }
        return httpclient.execute(httpost, responseHandler);
    }

ここでWCFファイル:

namespace MyWCFSolution
{

   [ServiceContract]
    public interface IService1
    {
       [OperationContract]
       [WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "Check")]
       String CheckSQL(string getJson);

    }
}

Jsonを使用してwcfサーバーをandroidに接続するにはどうすればよいですか。ユーザー名とパスワードを含むJsonオブジェクトと、ユーザー名、パスワード、名前、姓を含むjsonオブジェクトを含む応答を送信したい。しかし、その点で問題があります。ホストに接続できず、json データを投稿および取得できません。誰かが明確に説明できますか?(サンプルコード、コメント)

4

2 に答える 2

1

この記事を読んで、

http://www.vogella.com/articles/AndroidJSON/article.html

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

ObjectMapper mapper = new ObjectMapper();
ArrayList<RespuestaEncuesta> respuestas = new ArrayList<RespuestaEncuesta>(1);
RespuestaEncuesta r = new RespuestaEncuesta();
r.Comentarios = "ASD";
r.GrupoClienteID = UUID.fromString("00000000-0000-0000-0000-000000000000");
r.GrupoID = 1155;
r.Opcion = "2";
respuestas.add(r);

RespuestaWrapper data = new RespuestaWrapper();
data.Respuestas = respuestas;

mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
String respuestarJson = mapper.writeValueAsString(data);
String url = config[0] + "/GuardaEncuestas";

HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

StringEntity tmp = new StringEntity(respuestarJson);
httpPost.setEntity(tmp);

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.execute(httpPost);

それが役立つことを願っています

于 2013-01-11T19:59:37.680 に答える
0

私はあなたが必要とする解決策のように、少し前に何かを作りました。

それが最善の回避策であったかどうかはわかりませんが、完全に機能しました

メソッドでは、まず、JSONを受信する場合、「GET」は最適なオプションではありません。URLQueryStringには文字数制限があり、JSONが大きい場合は、そこに入力されないため、POSTを試してください。

次に、JSON構造と完全に一致するPOCOオブジェクトをNetで宣言し、次にそのオブジェクトを受信する必要があることをメソッドで宣言しました。

jsonのキーがPOCOオブジェクトのプロパティと完全に一致することが重要であり、キーに依存します

IISは、Androidからネット内のそのPOCOオブジェクトに投稿されたJSONを自動的に解析したため、魔法のように機能し、解析するものはなく、クリーンなオブジェクトを受け取りました。

もちろん、URIテンプレートに問題がないかどうかを確認してください。問題がない場合は、WCFサーバーでリクエストを受信することはありません。

その他:Android APPでは、JSONをPOSTリクエストとして送信しています。WCFで、メソッドはGET要求を待機しています。

于 2013-01-11T20:12:15.300 に答える