在android中使用google令牌执行HttpPost不获取JSON数据


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();
        }

首先你不能在http post中传递这样的参数,使用下面的代码为您使用,可能会有一些编译错误,因为我不使用和IDE检查我发布的代码,主要是向您展示如何使用http post传递post参数,这是通过使用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();
        }

POST和GET方法有区别…

在GET方法中你可以用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..........

这是认证的发布。这将返回一个包含访问令牌的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();
    }

相关内容

  • 没有找到相关文章

最新更新