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 post 中传递这样的参数,使用下面的代码供你使用,可能会有一些编译错误,因为我没有使用和 IDE 来检查正在发布的代码,重点是向你展示如何使用 http 传递 post 参数使用 NameValuePairs 的帖子,请相应调整您的网址

        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 传递数据......您必须将其作为实体传递............为将数据传递到执行以下操作网址

尝试这个..

      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

这是用于认证的帖子。这将返回一个 HttpResponse ,其中包含您在执行 HttpGet 请求时将使用的访问令牌。

  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 回答