1
HttpPost httpPost = new HttpPost("MyWebsiteURL");
        try {
            httpPost.setEntity(new StringEntity("https://www.googleapis.com/plus/v1/people/me?key="+token));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String json = sb.toString();
            Log.i("JSON", json);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
4

3 に答える 3

1

まず、http 投稿でこのようなパラメーターを渡すことはできません。以下のコードを使用してください。投稿しているコードをチェックするために IDE を使用していないため、コンパイル エラーが発生する可能性があります。主なポイントは、http を使用して投稿パラメーターを渡す方法を示すことです。 NameValuePairs を使用した投稿です。それに応じて URL を調整してください。

        try {
           HttpPost httppost = new HttpPost("https://www.googleapis.com/plus/v1/people/me");
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
           nameValuePairs.add(new BasicNameValuePair("key", "12345"));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream is = httpEntity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String json = sb.toString();
            Log.i("JSON", json);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
于 2014-07-31T06:29:04.850 に答える
0

POSTGETメソッドには違いがあります...

GET METHOD では URL を使用してデータを渡すことができます....
しかし、POST メソッドでは URL を使用してデータを渡すことはできません...エンティティとして渡す必要があります.... URL

これを試して..

      DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httpPost = new HttpPost(WEBSITE_URL);
        try{    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            //use this to pass variables while using POST method
            // add an HTTP variable and value pair
            nameValuePairs.add(new BasicNameValuePair("key name","key value")); 
            nameValuePairs.add(new BasicNameValuePair("key name","key value"));
            nameValuePairs.add(new BasicNameValuePair("key name","key value"));

            // passing data to the URL
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            //do the following..

           }

あなたのソリューションのためにあなたは合格する必要があります

nameValuePairs.add(new BasicNameValuePair("key",token));

どこ、

 Key - variable or key you defined in Your page (Backend side)     
 token = is a value which you want to pass..........
于 2014-07-31T06:37:12.783 に答える
0

認証用の投稿です。これにより、HttpGet リクエストを実行するときに使用するアクセス トークンを含む HttpResponse が返されます。

  try {
        Log.i(tag, "Starting doHTTPPost");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("KEY", "VALUE"));

        /* EXAMPLE of pairs */
        pairs.add(new BasicNameValuePair("client_id","theGreatSecret12345"));
        pairs.add(new BasicNameValuePair("username", "purple"));
        pairs.add(new BasicNameValuePair("password", "asjdf098732hkndfa"));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("The API Server you want to access");
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = client.execute(post);
        HttpResponse apiResponse = response;
        String resultFromServerAsAString = EntityUtils.toString(response.getEntity());

        Log.i(tag, "Response statusCode : "+response.getStatusLine().getStatusCode());
        Log.i(tag, "Response StatusLine : "+response.getStatusLine());
        Log.i(tag, "Ending doHTTPPost");

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2015-01-13T10:42:30.717 に答える