0

このWeb サービスにデータを投稿しようとしていますが、うまくいかないようです。だから私はjsonをサーバーに投稿しようとしていますが、その方法がわかりません。json 応答を取得するには、この例の json を送信する必要があります。

Content-Type : application/json HTTPMethod : POST HTTPBody : {"CouponVerificationCode":"594952223490","ApiKey":"zFyWQDYUKXQQpvG86snPD1OSslr7Q6DGEGbQ1f7P2YeTxB56y","Token":"_2_jx1YFvTZGGLNtJBoDW3gDZmNNAGpTWzT7dC6GrNAIkhhX9PWv75b776gq1ZO_2_SxMJjq8_2_kaDMyxX59HczOyaw=="}

しかし、json 応答を取得する代わりに、html 応答を取得します。誰かがこの問題を解決するのを手伝ってくれますか?

これは、サーバーとの通信に使用するコードです。

public static String makeRequest(String path) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(path);
        JSONObject holder = new JSONObject();
        // holder.accumulate(name, value)


        String test = "{\"ApiKey\":\"somekey\","
                + "\"OperativeSystem\":\"0\","
                + "\"AppVersion\":\"1\","
                + "\"WebServiceVersion\":\"1\"}";
        StringEntity se = new StringEntity(test);
        Log.v("--", test);
        httpost.setEntity(se);
        httpost.setHeader("Accept", "application/json");
        httpost.setHeader("Content-type", "application/json");

        ResponseHandler responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httpost, responseHandler);
        // HttpEntity entity = response.getEntity();
        Log.v("--", response);
        return response;
    }
4

1 に答える 1

0

httpPost の代わりに httpGet を使用してみてください。ここに私の作業コードがあります:

            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpGet httpGet =new HttpGet(url);
            String User =  context.getString(R.string.json_uname);
            String Password =  context.getString(R.string.json_pwd);
            httpGet.setHeader("Authorization", "Basic "+ new String(Base64.encode((User +":"+ Password).getBytes(),Base64.NO_WRAP )));

            HttpResponse httpResponse = httpclient.execute(httpGet);

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
            {
                //convertInputStreamToString(inputStream);
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                String json = reader.readLine();
                JSONTokener tokener = new JSONTokener(json);
                 finalResult = new JSONArray(tokener);
            }
            else
                 finalResult =null;
于 2014-02-26T18:19:38.753 に答える